2015-11-15 136 views
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> 

回答

0

你铸造适配器View是你的CheckBox。相反,你应该找到你的CheckBox致电

CheckBox cb = (CheckBox) view.findViewById(R.id.answertype_checkbox); 
+0

谢谢!使用您的重播我试图获得对CheckBox的引用,但无论我尝试我得到以下错误消息“java.lang.NullPointerException:尝试调用虚拟方法'void android.widget.CheckBox.setSelected(boolean)'on a空对象引用“。当此控件未绑定到游标/数据适配器时,是否有限制来获取对控件的引用? – Norge

相关问题