无法将值更新为复选框Android

时间:2013-11-19 12:15:17

标签: java android database sqlite checkbox

我正在尝试更新我的复选框,例如,如果patientPHistory为“是”,则患者复选框将打勾。除了这两个,我能够从数据库中检索所有值。 然而,有一些问题,例如当patientPHistory和patientPPhysical都是“是”时,两个复选框都会勾选,但当数据库中只有一个patientPHistory或patientPPhysical为“是”时,所有复选框都不会勾选。

对于示例数据库图像,它在此链接中:http://imgur.com/kHfZPkE

例如,B.K.K将检查两个复选框,但B.K.Z.W和T.Y.Y即使有“是”也不会。

private CheckBox History, Physical;

    private void RetrieveDataFromDatabase(String res)
{
    patientPHistory = "";
    patientPPhysical = "";
    myContext = this;
    patientDB = new PatientDB(myContext);
    patientDB.open();

    Cursor patientCursor;

    patientCursor = patientDB.retrieveAllBasedOnInitials(res);

    if(patientCursor!=null && patientCursor.getCount()>0)
    {
        patientCursor.moveToFirst();
        do 
        {
            patientCursor.getString(patientDB.COLUMN_KEY_ID); // + " " + 
            patientInitials = patientCursor.getString(patientDB.COLUMN_INITIALS_ID);
            patientDate = patientCursor.getString(patientDB.COLUMN_DATE_ID);
            patientSite = patientCursor.getString(patientDB.COLUMN_SITE_ID);
            patientWard = patientCursor.getString(patientDB.COLUMN_WARD_ID);
            patientPHistory = patientCursor.getString(patientDB.COLUMN_PERF_HISTORY_ID);
            patientPPhysical = patientCursor.getString(patientDB.COLUMN_PERF_PHYSICAL_ID);
            Log.i("ward", patientWard);
            Log.i("date", patientDate);
            Log.i("site", patientSite);
            Log.i("history", patientPHistory);
            Log.i("physical", patientPPhysical);

            //SET THEM
            DateOfDiagnosis.setText(patientDate);
            Ward.setText(patientWard);

            if (patientPHistory.contains("Yes"))
            {
                History.setChecked(true);
            }
            else if (patientPHistory.contains("No"))
            {
                History.setChecked(false);
            }

            if (patientPPhysical.contains("Yes"))
            {
                Physical.setChecked(true);
            }
            else if (patientPPhysical.contains("No"))
            {
                Physical.setChecked(false);
            }

            //Set to spinner
            List<String> newlist = new ArrayList<String>();
            newlist.add(patientSite);
            ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, newlist);
            dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            SpinnerSite.setAdapter(dataAdapter);
        } 
        while (patientCursor.moveToNext()); 
    }
}

2 个答案:

答案 0 :(得分:0)

如果您只有是或没有值,那么只使用else而不是else 即。

if (patientPHistory.contains("Yes"))
            {
                History.setChecked(true);
                Log.i("checked or not","yes");
            }
            else
            {
                History.setChecked(false);
                Log.i("checked or not","no");
            }

答案 1 :(得分:0)

我意识到我的光标有两个相同的实体导致了这个错误,感谢帮助它是我的疏忽。