Javascript 使用setViewValue中其他列的值设置复选框
我从sqlite检索数据,并使用游标适配器将其填充到listview中。我使用setViewValue检索名为“ShowAs”的列的值。“ShowAs”可以是“复选框”或“微调器”。例如,当值为“checkbox”时,我需要找到该复选框并设置其值并隐藏微调器 问题是我无法从“ShowAs”列(这本身就是一个文本框)中找到复选框(列)。请注意,复选框(和微调器)未绑定到dbJavascript 使用setViewValue中其他列的值设置复选框,javascript,android,android-checkbox,Javascript,Android,Android Checkbox,我从sqlite检索数据,并使用游标适配器将其填充到listview中。我使用setViewValue检索名为“ShowAs”的列的值。“ShowAs”可以是“复选框”或“微调器”。例如,当值为“checkbox”时,我需要找到该复选框并设置其值并隐藏微调器 问题是我无法从“ShowAs”列(这本身就是一个文本框)中找到复选框(列)。请注意,复选框(和微调器)未绑定到db private void displayListView() { Cursor cursor = dbHelper
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>
private void displayListView(){
Cursor Cursor=dbHelper.fetchAllAnswers(surveyID);
//要绑定的所需列
字符串[]列=新字符串[]{
DBAdapter.KEY_ANSWERID,
DBAdapter.KEY_答案,
DBAdapter.KEY_SHOWASID,
DBAdapter.KEY\u帮助文本
};
//数据将绑定到的XML定义的视图
int[]至=新int[]{
R.id.answerid,
R.id.答案,
R.id.showasid,
R.id.helptext,
};
dataAdapter=新的SimpleCursorAdapter(this,R.layout.answer\u info,游标,列,to,0);
ListView ListView=(ListView)findViewById(R.id.listViewAnswers);
//将适配器分配给ListView
setAdapter(dataAdapter);
//隐藏或显示正确的控件和设置值
dataAdapter.setViewBinder(新的SimpleCursorAdapter.ViewBinder(){
@凌驾
公共布尔setViewValue(视图、光标、int-columnIndex){
if(view.getId()==R.id.showasid){
String answerType=cursor.getString(cursor.getColumnIndex(“ShowAsID”);
开关(应答型){
案例“微调器”:
Log.d(“ShowAsID”、“Spinner”);
打破
案例“编号”:
Log.d(“ShowAsID”,“.Number”);
打破
案例“文本框”:
Log.d(“ShowAsID”,“.TextBox”);
打破
案例“复选框”:
Log.d(“ShowAsID”和“.Checkbox”);
//在这里,我试图找到复选框。。
复选框cb=(复选框)视图;
cb.setChecked(真);
}
返回true;
}
返回false;
}
});
}
您正在将适配器视图
转换为您的复选框
。相反,您应该通过调用
CheckBox cb = (CheckBox) view.findViewById(R.id.answertype_checkbox);
谢谢使用您的重播,我试图获取对复选框的引用,但无论我如何尝试,都会得到以下错误消息“java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'void android.widget.CheckBox.setSelected(boolean)”。当该控件未绑定在游标/数据适配器中时,获取对该控件的引用是否存在限制?