使用setViewValue

时间:2015-11-15 07:33:12

标签: javascript android android-checkbox

我从sqlite检索数据并使用cursoradapter在listview中填充它。使用setViewValue我检索名为" ShowAs"的列的值。 " ShowAs"可以是"复选框"或者" spinner"。例如,当值为"复选框"我需要找到复选框并设置它的值并隐藏微调器。

问题在于我无法从列#34; ShowAs"中找到复选框(列)。 (这是一个文本框本身)。请注意,checkbox(和spinner)未绑定到db。

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>

1 个答案:

答案 0 :(得分:0)

您正在将适配器View转换为CheckBox。相反,您应该通过调用

找到CheckBox
CheckBox cb = (CheckBox) view.findViewById(R.id.answertype_checkbox);