2012-04-19 20 views
0

我的自定義適配器:Android的ListView中選中所有複選框(定製ResourceCursorAdapter)在活動

private static HashMap<Integer, Boolean> checkedMap; 

private class MyMediaCursorAdapter extends ResourceCursorAdapter { 

    public MyMediaCursorAdapter(Context context, int layout, Cursor c) { 
     super(context, layout, c); 

    } 

    @Override 
    public View newView(Context context, Cursor cursor, ViewGroup parent) { 
     View view = super.newView(context, cursor, parent); 
     final RecordListItemCache cache = new RecordListItemCache(); 

     cache.date = (TextView)view.findViewById(R.id.dateRecorded); 
     cache.checkBox = (CheckBox)view.findViewById(R.id.checkBoxView);  

     view.setTag(cache);   
     return view; 
    } 

    @Override 
    public void bindView(View view, Context context, Cursor cursor) { 
     final RecordListItemCache cache = (RecordListItemCache)view.getTag(); 

     final SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss"); 
     final String dateString = sdf.format(cursor.getLong(cursor 
       .getColumnIndex(MediaStore.Audio.Media.DATE_ADDED))*1000); 

     final String displayName = cursor.getString(cursor 
       .getColumnIndex(MediaStore.Audio.Media.TITLE)); 

     final int id = cursor.getInt(cursor 
       .getColumnIndex(MediaStore.Audio.Media._ID)); 
     cache.checkBox.setOnCheckedChangeListener(null); 
     Boolean chk = checkedMap.get(id); 
     if (chk==null) { 
      cache.checkBox.setChecked(false); 
     } else { 
      cache.checkBox.setChecked(chk); 
     } 


     cache.checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() { 
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
       checkedMap.put(id, isChecked); 

      } 
     }); 

     cache.checkBox.setText(displayName); 
     cache.date.setText(dateString); 
    } 
} 

final static class RecordListItemCache { 
    public CheckBox checkBox; 
    public TextView date; 
    public boolean checked; 
} 

在的onCreate在同一個活動:

myMediaCursorAdapter = new MyMediaCursorAdapter(getApplicationContext(), 
      R.layout.multipledeleteview, audioCurosr); 
setListAdapter(myMediaCursorAdapter); 

和的onClick:

public void onClick(View button) { 
    switch (button.getId()) { 
     case R.id.buttonDeleteMultiple: { 
      Collection<Integer> IDs = checkedMap.keySet(); 
      for (Integer id : IDs) { 
       if (checkedMap.get(id)==true) { 
        RecorderUtils.delete(getApplicationContext(), id, false); 
       } 
      } 
      checkedMap = new HashMap<Integer, Boolean>(); 
     } 
     case R.id.buttonDeleteMultipleChooseAll: { 
      int size = myMediaCursorAdapter.getCount(); 
      ListView lv = getListView(); 
      for(int i = 0; i <= size; i++) { 
       lv.setItemChecked(i, true); 
      } 
      myMediaCursorAdapter.notifyDataSetChanged(); 
     } 
    } 
} 

刪除工作正常(它刪除檢查的人)。但選擇所有按鈕並不會改變任何東西。

回答

0

我使用勾選的複選框重新加載視圖...

相關問題