Android::Data are not retrieving to EditText field through Database

老子叫甜甜 提交于 2019-12-13 05:15:21

问题


I had gone through all the post regarding accessing data from database although I can't be able to solve my problem so I am posting my problem in this site. I want to access the SQL table which has been previously stored. My code works fine with storing of data but when I click on the save button but it is not working when I pressed on the Load button means it is inserting null values rather then stored values.

Here are my code..

public class SetValue extends Fragment {




    @Override
    public void onSaveInstanceState(Bundle outState) {
        // TODO Auto-generated method stub
        super.onSaveInstanceState(outState);
        populateData();
    }

    Button save, Load;
    EditText firstName, name, patientID, minH, maxH, minI, maxI, minB, maxB;
    private String nameofP,FName,pID;
    private String HRM,HRX,IBIM,IBIX,BRM,BRX;
    private String nameET,firstNameET,pIDET;
    private String minHET,maxHET,minIET,maxIET,minBET,maxBET;
    Patient patient=new Patient();

    @Override
    public void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        populateData();
    }

    @SuppressWarnings("deprecation")
    DatabaseHandler populateData() {
        // TODO Auto-generated method stub
        DatabaseHandler mDatabase= new DatabaseHandler(getActivity());

        SQLiteDatabase sql=mDatabase.getReadableDatabase();
        Cursor cursor = sql.query(mDatabase.NAME_TABLE, null, null, null, null, null, null);
    getActivity().startManagingCursor(cursor);
        while (cursor.moveToNext()) {

            String cFirstname = cursor.getString(cursor.getColumnIndex( mDatabase.KEY_NAME));
            String cName      = cursor.getString(cursor.getColumnIndex( mDatabase.KEY_HR));
            String cPID = cursor.getString(cursor.getColumnIndex( mDatabase.KEY_IBI));
            String cHeartRateMin = cursor.getString(cursor.getColumnIndex( mDatabase.KEY_HMIN));
            String cHeartRateMax = cursor.getString(cursor.getColumnIndex( mDatabase.KEY_HMAX));
            String cIBIMin = cursor.getString(cursor.getColumnIndex( mDatabase.KEY_IMIN));
            String cIBIMax = cursor.getString(cursor.getColumnIndex( mDatabase.KEY_IMAX));
            String cBreathingRateMin = cursor.getString(cursor.getColumnIndex( mDatabase.KEY_BMIN));
            String cBreathingRateMax = cursor.getString(cursor.getColumnIndex( mDatabase.KEY_BMAX));

            Patient currentPatient= new Patient();
            currentPatient.setName(cName);
            currentPatient.setFirstName(cFirstname);
            currentPatient.setId(cPID);
            currentPatient.setNumHeartRateMin(cHeartRateMin);
            currentPatient.setNumHeartRateMax(cHeartRateMax);
            currentPatient.setNumInterBeatIntervalMin(cIBIMin);
            currentPatient.setNumInterBeatIntervalMax(cIBIMax);
            currentPatient.setNumBreathingrateMin(cBreathingRateMin);
            currentPatient.setNumBreathingrateMax(cBreathingRateMax);

        }
        sql.close();
        return mDatabase;

    }

DatabaseHandler dbhandler = new DatabaseHandler(getActivity());

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        final View myFragmentView = inflater.inflate(
        R.layout.set_value_fragment, container, false);


        save = (Button) myFragmentView.findViewById(R.id.Save);
        save.setOnClickListener(onClickSave(myFragmentView));
        Load = (Button) myFragmentView.findViewById(R.id.btn4);
        Load.setOnClickListener(onClickLoad(myFragmentView));

        firstName=(EditText)myFragmentView.findViewById(R.id.FN);
        name=(EditText)myFragmentView.findViewById(R.id.name);
        patientID=(EditText)myFragmentView.findViewById(R.id.PID);
        minH=(EditText)myFragmentView.findViewById(R.id.Hmin);
        maxH=(EditText)myFragmentView.findViewById(R.id.Hmax);
        minI=(EditText)myFragmentView.findViewById(R.id.Imin);
        maxI=(EditText)myFragmentView.findViewById(R.id.Imax);
        minB=(EditText)myFragmentView.findViewById(R.id.Bmin);
        maxB=(EditText)myFragmentView.findViewById(R.id.Bmax);

        return myFragmentView;
    }

    private OnClickListener onClickLoad(View myFragmentView) {
        // TODO Auto-generated method stub
        return new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                firstName.setText(nameofP); 
                name.setText(FName); 
                patientID.setText(pID);
                minH.setText(String.valueOf(HRM));
                maxH.setText(String.valueOf(HRX));
                minI.setText(String.valueOf(IBIM));
                maxI.setText(String.valueOf(IBIX));
                minB.setText(String.valueOf(BRM));
                maxB.setText(String.valueOf(BRX));
                updateDetails(patient);

            }

            private void updateDetails(Patient patient) {
                // TODO Auto-generated method stub
                DatabaseHandler androidDbHelper = new DatabaseHandler(getActivity());
                SQLiteDatabase sql=androidDbHelper.getWritableDatabase();
                ContentValues contentValues = new ContentValues();
                contentValues.put(androidDbHelper.KEY_NAME, nameET);
                contentValues.put(androidDbHelper.KEY_HR, firstNameET);
                contentValues.put(androidDbHelper.KEY_IBI, pIDET);
                contentValues.put(androidDbHelper.KEY_HMIN, minHET);
                contentValues.put(androidDbHelper.KEY_HMAX, maxHET);
                contentValues.put(androidDbHelper.KEY_IMIN, minIET);
                contentValues.put(androidDbHelper.KEY_IMAX, maxIET);
                contentValues.put(androidDbHelper.KEY_BMIN, minBET);
                contentValues.put(androidDbHelper.KEY_BMAX, maxBET);

                String[] clauseArgument= new String[1];
                clauseArgument[0]=patient.getName();
                System.out.println("whereClauseArgument[0] is :" + clauseArgument[0]);

                sql.update(androidDbHelper.NAME_TABLE, contentValues, androidDbHelper.KEY_NAME+"=?", clauseArgument);
                sql.close();

            }

        };
    }

    private OnClickListener onClickSave(final View myFragmentView) {
        return new OnClickListener() {

            /**
             * 
             * @see android.view.View.OnClickListener#onClick(android.view.View)
             */
            @Override
            public void onClick(View v) {

                //set the values that we extracted
                nameET=firstName.getText().toString();
                firstNameET=name.getText().toString();
                pIDET=patientID.getText().toString();
                minHET=minH.getText().toString();
                maxHET= maxH.getText().toString();
                minIET= minI.getText().toString();
                maxIET=maxI.getText().toString();
                minBET=minB.getText().toString();
                maxBET=maxB.getText().toString();


                patient.setName(nameofP);
                patient.setFirstName(FName);
                patient.setId(pID);
                patient.setNumHeartRateMin(HRM);
                patient.setNumHeartRateMax(HRX);
                patient.setNumInterBeatIntervalMin(IBIM);
                patient.setNumInterBeatIntervalMax(IBIX);
                patient.setNumBreathingrateMin(BRM);
                patient.setNumBreathingrateMax(BRX);
                insertData(patient);

            }

            public void insertData(Patient patient) {
                DatabaseHandler androidDbHelper = new DatabaseHandler(getActivity());
                SQLiteDatabase sql=androidDbHelper.getWritableDatabase();
                ContentValues contentValues = new ContentValues();
                contentValues.put(androidDbHelper.KEY_NAME, patient.getName());
                contentValues.put(androidDbHelper.KEY_HR, patient.getFirstName());
                contentValues.put(androidDbHelper.KEY_IBI, patient.getId());
                contentValues.put(androidDbHelper.KEY_HMIN, patient.getNumHeartRateMin());
                contentValues.put(androidDbHelper.KEY_HMAX, patient.getNumHeartRateMax());
                contentValues.put(androidDbHelper.KEY_IMIN, patient.getNumInterBeatIntervalMin());
                contentValues.put(androidDbHelper.KEY_IMAX, patient.getNumInterBeatIntervalMax());
                contentValues.put(androidDbHelper.KEY_BMIN, patient.getNumBreathingrateMin());
                contentValues.put(androidDbHelper.KEY_BMAX, patient.getNumBreathingrateMax());
                long columnID=sql.insert(androidDbHelper.NAME_TABLE, null, contentValues);
                sql.close();
                Toast.makeText(getActivity().getBaseContext(), "Values inserted column ID is :" + columnID, Toast.LENGTH_SHORT).show();

            }
        };
    }

}

Here is my DatadaseHandler.java

public class DatabaseHandler extends SQLiteOpenHelper {

    // Database fields
    public SQLiteDatabase db;

    public DatabaseHandler(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }
private static final int DATABASE_VERSION = 3;

    // Database Name
    private static final String DATABASE_NAME = "dataManager";
    public static final String NAME_TABLE = "nametable";

    // Contacts table name

    public static final String KEY_NAME = "name";
    public static final String KEY_HR = "firstName";
    public static final String KEY_IBI = "patientID";
    public static final String KEY_HMIN = "MinimumofHeartRate";
    public static final String KEY_HMAX = "MaximumofHeartRate";
    public static final String KEY_IMIN = "MinimumofIBI";
    public static final String KEY_IMAX = "MaximumofIBI";
    public static final String KEY_BMIN = "MinimumofBreathingRate";
    public static final String KEY_BMAX = "MaximumofBreathingRate";

    private static final String TEXT_TYPE = " TEXT";
    private static final String COMMA_SEP = ", ";


    // Creating Tables
    static final String DATABASE_CREATE_FIRST = "CREATE TABLE IF NOT EXISTS "
            + NAME_TABLE + "(" + KEY_NAME + TEXT_TYPE + COMMA_SEP + KEY_HR
            + TEXT_TYPE + COMMA_SEP + KEY_IBI + TEXT_TYPE + COMMA_SEP
            + KEY_HMIN + TEXT_TYPE + COMMA_SEP + KEY_HMAX + TEXT_TYPE
            + COMMA_SEP + KEY_IMIN + TEXT_TYPE + COMMA_SEP + KEY_IMAX
            + TEXT_TYPE + COMMA_SEP + KEY_BMIN + TEXT_TYPE + COMMA_SEP
            + KEY_BMAX + TEXT_TYPE + ")";

    @Override
    public void onCreate(SQLiteDatabase db) {

        db.execSQL(DATABASE_CREATE_FIRST);

    }


    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

        db.execSQL("DROP TABLE IF EXISTS " + DATABASE_CREATE_FIRST);
        onCreate(db);


    }

And here is my getter and setter variables::patient.java

public class Patient {
    private String id;
    private String firstName;
    private String name;
    private String numHeartRateMin, numHeartRateMax, numInterBeatIntervalMin, numInterBeatIntervalMax,
                     numBreathingrateMin, numBreathingrateMax;

    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getFirstName() {
        return firstName;
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getNumHeartRateMin() {
        return numHeartRateMin;
    }
    public void setNumHeartRateMin(String numHeartRateMin) {
        this.numHeartRateMin = numHeartRateMin;
    }
    public String getNumHeartRateMax() {
        return numHeartRateMax;
    }
    public void setNumHeartRateMax(String numHeartRateMax) {
        this.numHeartRateMax = numHeartRateMax;
    }
    public String getNumInterBeatIntervalMin() {
        return numInterBeatIntervalMin;
    }
    public void setNumInterBeatIntervalMin(String numInterBeatIntervalMin) {
        this.numInterBeatIntervalMin = numInterBeatIntervalMin;
    }
    public String getNumInterBeatIntervalMax() {
        return numInterBeatIntervalMax;
    }
    public void setNumInterBeatIntervalMax(String numInterBeatIntervalMax) {
        this.numInterBeatIntervalMax = numInterBeatIntervalMax;
    }
    public String getNumBreathingrateMin() {
        return numBreathingrateMin;
    }
    public void setNumBreathingrateMin(String numBreathingrateMin) {
        this.numBreathingrateMin = numBreathingrateMin;
    }
    public String getNumBreathingrateMax() {
        return numBreathingrateMax;
    }
    public void setNumBreathingrateMax(String numBreathingrateMax) {
        this.numBreathingrateMax = numBreathingrateMax;
    }

}

I know, I put long code but believe me these are necessary to describe my problem...


回答1:


You are inserting Null Strings in Pacient and saving on the DB.

First:

Create a Global Pacient:

Patient currentPatient= new Patient();

After, in onClickLoad:

 @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub  
            firstName.setText(currentPatient.getName());
            name.setText(currentPatient.getFirstName());
            patientID.setText(currentPatient.getId());
            minH.setText(currentPatient.getNumHeartRateMin());
            maxH.setText(currentPatient.getNumHeartRateMax());
            minI.setText(currentPatient.getNumInterBeatIntervalMin());
            maxI.setText(currentPatient.getNumInterBeatIntervalMax());
            minB.setText(currentPatient.getNumBreathingrateMin());
            maxB.setText(currentPatient.getNumBreathingrateMax());
            updateDetails(patient);

        }

This fix the save action.

    nameET=firstName.getText().toString();
    firstNameET=name.getText().toString();
    pIDET=patientID.getText().toString();
    minHET=minH.getText().toString();
    maxHET= maxH.getText().toString();
    minIET= minI.getText().toString();
    maxIET=maxI.getText().toString();
    minBET=minB.getText().toString();
    maxBET=maxB.getText().toString();


    patient.setName(nameET);
    patient.setFirstName(firstNameET);
    patient.setId(pIDET);
    patient.setNumHeartRateMin(minHET);
    patient.setNumHeartRateMax(maxHET);
    patient.setNumInterBeatIntervalMin(minIET);
    patient.setNumInterBeatIntervalMax(maxIET);
    patient.setNumBreathingrateMin(minBET);
    patient.setNumBreathingrateMax(maxBET);
    insertData(patient);


来源:https://stackoverflow.com/questions/19099789/androiddata-are-not-retrieving-to-edittext-field-through-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!