【问题标题】:Cannot update value to checkbox Android无法将值更新为复选框 Android
【发布时间】:2013-12-02 23:48:50
【问题描述】:

我正在尝试更新我的复选框,例如如果 patientPHistory 为“是”,则 Patient 复选框将是一个勾号。我能够从数据库中检索除这两个之外的所有值。 但是有一些问题,例如当 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()); 
    }
}

【问题讨论】:

  • 你不认为在你的情况下应该是“patientPPhysical.equals("NO")”吗?
  • 我想你还没有初始化History。你必须在访问它之前做这样的事情Hitory = (CheckBox) findViewById(R.id.historyid);
  • Vishwas,我尝试了 .matches .equals 和 .contains - 如果我的两个数据都包含一个是,则所有这些都不起作用
  • Abhishek,我可以检查身体和历史是否都是肯定的,但当其中一个是肯定时则不能
  • 所以你的问题是斯宾塞。您的数据库返回错误信息。大多数情况下,您对数据库的输入是错误的。对于 B.K.Z.W,它应该是“是”和“否”。

标签: java android database sqlite checkbox


【解决方案1】:

如果您只有 yes 或 no 值,则仅使用 else 而不是 else if 即

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

【讨论】:

  • 仅在物理和历史都为“是”时有效,但在其中一个为“是”且其中一个为“否”时无效
  • 您是否检查过您的日志,当其中一个为“是”且其中一个为“否”时,您确定日志会为您提供正确的数据
【解决方案2】:

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

【讨论】:

  • 另外,adapter 代码不应该在do-while 循环之外吗?这部分//Set to spinner List&lt;String&gt; newlist = new ArrayList&lt;String&gt;(); newlist.add(patientSite); ArrayAdapter&lt;String&gt; dataAdapter = new ArrayAdapter&lt;String&gt;(this, android.R.layout.simple_spinner_item, newlist); dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); SpinnerSite.setAdapter(dataAdapter);
猜你喜欢
  • 1970-01-01
  • 2016-04-25
  • 1970-01-01
  • 2018-04-05
  • 1970-01-01
  • 2012-04-20
  • 1970-01-01
  • 2017-12-18
  • 1970-01-01
相关资源
最近更新 更多