2013-03-21 103 views
0

我在獲取ListView上的檢查項目時遇到問題。 事情是,無論什麼時候我調用getCheckedItemsCount()或getCheckedItemPositions()它總是返回1.無論是否有0或2或更多的項目檢查。無法從ListView中獲取實際檢查的項目

這是我的MainActivity,它實現MultiChoiceModeListener,以偵聽項目何時被選中。 我這樣做是因爲我在ListAdapter上動態檢查項目。

public class MainActivity extends ListActivity implements MultiChoiceModeListener 
{ 
    @Override 
    protected void onCreate (Bundle bundle) 
    { 
     super.onCreate (bundle); 
     // Set our view from the "main" layout resource 
     setContentView (R.layout.main); 

     this.getListView().setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE_MODAL); 
     this.getListView().setMultiChoiceModeListener(this); 

     _dataAdapter = new ServerListAdapter (this); 
     this.getListView(). setAdapter(_dataAdapter); 
     registerForContextMenu (this.getListView()); 
    } 

    // This is called when i set the item as checked using setItemChecked on my ListAdapter. 
    public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean isChecked) { 


     SparseBooleanArray checke = getListView().getCheckedItemPositions(); 
     // This always returns 1. No matter If I have 0 or 2 items checked. 
     int checkedCount = checkedItemPositions.size(); 

     // I Have also tried with getCheckedItemsCount() and it returns 1 too. 

     if (checkedCount > 0) 
     { 
      // DO SOMETHING... 
     } 
     else 
     { 
       // DO SOME OTHER STUFF... 
     } 
    } 

這裏是我的ListAdapter的代碼。只有相關的代碼是在這裏:

public class ServerListAdapter extends BaseAdapter { 
    @Override 
    public View getView (final int position, final View convertView, final ViewGroup parent) 
    { 
     final ListView listView = (ListView)parent; 
     boolean isChecked = listView.isItemChecked(position); 

     ((CheckBox)view.findViewById(R.id.chkItemServerSelected)).setOnCheckedChangeListener(new OnCheckedChangeListener() 
     { 


      @Override 
      public void onCheckedChanged(CompoundButton pCompound, boolean arg1) 
      { 

       boolean isChecked = listView.isItemChecked(position); 
       // Here i set the item as checked, o unchecked. This works ok. 
       listView.setItemChecked(position, !isChecked); 
      } 

     }); 

     //Finally return the view 
     return view; 
    } 
} 

編輯:

環顧四周後,我發現的問題是,我是做了錯誤的方式。

在我的列表適配器上onCheckedChanged,而不是使用從列表視圖獲取當前值,我不得不使用複選框中的值(因爲這是我想要實現的)。

以前的代碼:

listView.setItemChecked(position, !isChecked); 

新代碼:

listView.setItemChecked(position, pCompound.isChecked()); 

的事情是,這帶來了新的問題。 當選中的值IsChecked爲TRUE時,會引發onItemCheckedStateChanged事件,但當值爲時FALSE它不......任何線索?

+0

什麼排佈置您使用的?它不看它實現可檢查... – Sam 2013-03-21 17:17:30

+0

@Sam我發現了問題,但這帶來了一個新問題。請看看編輯過的帖子。我正在使用包含複選框的自定義佈局。 – 2013-03-21 18:33:58

回答

0

的問題是,ListView控件實例是越來越全亂了這樣而不是使用項目列表視圖的檢查值的,我用了CheckBox控件的ckeched值。

這一變化是在ListAdapter做出的複選框的onCheckedChanged方法:

以前的代碼:

listView.setItemChecked(position, !isChecked); 

新代碼:

listView.setItemChecked(position, pCompound.isChecked()); 
0

我相信你在基礎適配器中缺少一些覆蓋。我認爲你需要實現這樣的事情:(除了getView)

@Override 
    public int getCount() { 
      return myarray.size(); 
    } 

    @Override 
    public Object getItem(int position) { 
      return myarray.get(position); 
    } 

    @Override 
    public long getItemId(int position) { 
      return myarray.get(position).getId(); 
    } 
+0

我發現了這個問題,但是這帶來了一個新問題。請看看編輯過的帖子。 我已經實現了這些覆蓋,但我只是放置了相關的代碼,因爲問題不在適配器本身。 – 2013-03-21 18:34:16