2011-03-27 89 views
0

下面的代碼創建一個ArrayAdapter,我使用onResume()中的ListView.setItemChecked()更改每個CheckedTextView的狀態。android-ListView和ArrayAdapter with simple_list_item_multiple_choice

這很好。

有沒有一種方法可以禁用(灰色)一些CheckedTextViews

我試過使用adapter.getView(position, null, listView),雖然這返回一個CheckedTextView對象,但將它設置爲setclickable(false)不會做任何事情。我試過改變它的文本和改變狀態,但它也沒有做任何事情。

我該如何做到這一點?

下面是正常工作的代碼:

adapter = new ArrayAdapter<String>(this, 
       android.R.layout.simple_list_item_multiple_choice, 
       getResources().getStringArray(R.array.mode_declarations_array)); 

    listView.setAdapter(adapter);  

    listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); 

    String learningSettings = settings.getString(Constants.LEARNING_SETTINGS, null); 

    int count = adapter.getCount(); 

    if (learningSettings != null) { 

     String[] values = learningSettings.split(","); 

     for (int i = 0; i< values.length; i++) { 

      boolean val = Boolean.parseBoolean(values[i]); 

      listView.setItemChecked(i, val); 

     } 

    }else { 
     for (int i = 0; i<count; ++i) { 
      listView.setItemChecked(i, true); 
     } 
    } 

回答

1

你創建一個自定義的適配器和覆蓋isEnabled功能。

class MyAdapter extends ArrayAdapter<String> { 

    public boolean areAllItemsEnabled() { 
     return false; 
    } 

    public boolean isEnabled(int position) { 
     // return false if position == position you want to disable 
    } 
}