2012-12-11 38 views
5

我想要一個Multiselected (checked) listview。當我選擇一個項目時,必須出現綠色複選標記。爲此我使用CheckedTextViewsListView從數據庫中獲取數據。我爲此使用SimpleCursorAdapter。 當你點擊按鈕時,選定的條目(ID)將被傳遞給下一個活動。CheckedTextView未勾選

我的問題是沒有出現CheckedTextView的複選標記。但這些ID將傳遞給下一個活動。 我在做什麼錯?如何解決它?

selecttest.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

     <Spinner 
     android:id="@+id/spinner_select_language" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" /> 

     <Button 
      style="@style/btn_Font" 
      android:id="@+id/selecttest_start" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/spinner_test" 
      android:onClick="onClick" 
      android:text="@string/selecttest_start" /> 

     <ListView 
      android:id="@+id/lv_lesson" 
      android:layout_width="match_parent" 
      android:layout_height="200dp" 
      android:fadeScrollbars="false" 
      android:choiceMode="multipleChoice" 
      android:layout_alignParentLeft="true" 
      android:cacheColorHint="#00000000" 
      android:layout_below="@+id/selecttest_start" > 
     </ListView> 

</RelativeLayout> 

dataset_ctv_lesson.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <CheckedTextView 
     style="@style/tv_Font" 
     android:id="@+id/ctv_lesson" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:text="@string/none" 
     android:checkMark="@drawable/ctv_state_checker" 
     /> 

</RelativeLayout> 

ctv_state_checker.xml

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 

    <item android:state_checked="false" 
      android:drawable="@drawable/btn_check_buttonless_off" /> 
    <item android:state_checked="true" 
      android:drawable="@drawable/btn_check_buttonless_on" /> 

</selector> 

SelectTestActivity.java

public class SelectTestActivity 
extends Activity 
implements OnItemSelectedListener 
{ 
    Database db; 
    SimpleCursorAdapter adaptercursor, lv_adaptercursor; 
    ListView lv_lesson; 

    // Arraylist for checked item in the lesson view 
    ArrayList<String> checkedlessons = new ArrayList<String>(); 

    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.selecttest); 

     // Create Database 
     db = new Database(this); 

     // Drop-Down-Menu to select a language 
     Spinner spinner_language = (Spinner) findViewById(R.id.spinner_select_language); 
     spinner_language.setOnItemSelectedListener(this); 

     Cursor cursor = db.createListViewCursor(); 

     String[] displaycolumn = new String[]{"language"}; 
     int[] displayview = new int[] {R.id.tv_language}; 

     adaptercursor = new SimpleCursorAdapter(this, R.layout.datasetlanguage, cursor, displaycolumn, displayview, 0); 
     spinner_language.setAdapter(adaptercursor); 

     // ListView to select a lesson 
     lv_lesson = (ListView) findViewById(R.id.lv_lesson); 

     cursor = db.createListViewLessonCursor(getSelectedItemIDFromSpinnerLanguage()); 

     displaycolumn = new String[]{"lesson"}; 
     int[] displayview2 = new int[] {R.id.ctv_lesson}; 

     lv_adaptercursor = new SimpleCursorAdapter(this, R.layout.dataset_ctv_lesson, cursor, displaycolumn, displayview2, 0); 
     lv_lesson.setAdapter(lv_adaptercursor); 

     lv_lesson.setOnItemClickListener(new ListView.OnItemClickListener() 
     { 
      @Override 
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
      { 
       if (!checkedlessons.contains(Long.toString(id))) 
       { 
        checkedlessons.add(Long.toString(id)); 

        // checked textview 
        lv_lesson.setItemChecked(position, true); 
       } 
       else 
       { 
        checkedlessons.remove(Long.toString(id)); 

        // unchecked textview 
        lv_lesson.setItemChecked(position, false);  
       } 
      } 
     }); 

     // Close the database 
     db.close(); 
    } 

回答

3
  1. 嘗試刪除您的CheckedTextView的樣式,我認爲您的樣式中的某些值會影響外觀。
  2. 刪除dataset_ctv_lesson.xml中的RelativeLayout,並且不需要更改單擊項目上的檢查狀態。 ListView可以自己維護檢查狀態。使用ListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE)啓用多選模式,並使用ListView.getCheckedItemPositions()獲取檢查的行位置。
+1

我刪除了RelativeLayout,它現在可以工作。非常感謝。 – kaschey

+0

@ user1891764你是否理解這個原因? – faylon

+0

SimpleCursorAdapter需要一個佈局文件和視圖來顯示遊標的列。佈局文件應該只包含這些視圖。是對的嗎? – kaschey