0
我從sqlite中檢索數據,並使用cursoradapter將其填充到列表視圖中。使用setViewValue我檢索名爲「ShowAs」的列的值。 「ShowAs」可以是「複選框」或「微調器」。當例如值爲「複選框」時,我需要找到複選框並設置它的值並隱藏微調器。使用setViewValue中其他列的值設置複選框
問題是我無法從列「ShowAs」(這是一個文本框本身)找到複選框(列)。請注意,複選框(和微調)沒有綁定到數據庫。
private void displayListView() {
Cursor cursor = dbHelper.fetchAllAnswers(surveyID);
// The desired columns to be bound
String[] columns = new String[]{
DBAdapter.KEY_ANSWERID,
DBAdapter.KEY_ANSWER,
DBAdapter.KEY_SHOWASID,
DBAdapter.KEY_HELPTEXT
};
// The XML defined views which the data will be bound to
int[] to = new int[]{
R.id.answerid,
R.id.answer,
R.id.showasid,
R.id.helptext,
};
dataAdapter = new SimpleCursorAdapter(this, R.layout.answer_info, cursor, columns, to, 0);
ListView listView = (ListView) findViewById(R.id.listViewAnswers);
// Assign adapter to ListView
listView.setAdapter(dataAdapter);
// Hide or show the correct controls and set values
dataAdapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
@Override
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
if (view.getId() == R.id.showasid) {
String answerType = cursor.getString(cursor.getColumnIndex("ShowAsID"));
switch (answerType) {
case "spinner":
Log.d("ShowAsID", "..Spinner");
break;
case "number":
Log.d("ShowAsID", "..Number");
break;
case "textbox":
Log.d("ShowAsID", "..TextBox");
break;
case "checkbox":
Log.d("ShowAsID", "..Checkbox");
// Here I try to find the checkbox..
CheckBox cb = (CheckBox) view;
cb.setChecked(true);
}
return true;
}
return false;
}
});
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/answerid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="tv_answerid" />
<TextView
android:id="@+id/answer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/answerid"
android:text="tv_answer" />
<TextView
android:id="@+id/showasid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/answer"
android:text="tv_showasid" />
<TextView
android:id="@+id/helptext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/showasid"
android:text="tv_helptext" />
<CheckBox
android:id="@+id/answertype_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/helptext"
android:text="chk" />
<Spinner
android:id="@+id/answertype_spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/answertype_checkbox"/>
</RelativeLayout>
謝謝!使用您的重播我試圖獲得對CheckBox的引用,但無論我嘗試我得到以下錯誤消息「java.lang.NullPointerException:嘗試調用虛擬方法'void android.widget.CheckBox.setSelected(boolean)'on a空對象引用「。當此控件未綁定到遊標/數據適配器時,是否有限制來獲取對控件的引用? – Norge