0

我知道有很多與自定義列表視圖與複選框相關的問題,但仍然在檢索所有選中項目時遇到了一些問題。自定義列表視圖與複選框

什麼我做的是:

從數據庫中顯示自定義的ListView(所有已發送短信的列表)。

允許用戶檢查各種列表項目。

當用戶按下刪除按鈕,我想刪除所有選中的項目(從數據庫以及視圖部分)

問題: 當我去我的活動,第一次和檢查一些項目,並刪除它,它工作正常。

但是,當我只需按下刪除按鈕後再次檢查一些項目,一些項目被選中,並得到一些氾濫,又有些其他項目都將被刪除..

我想我不能夠綁定ID和列表項完美..

編碼算法迄今完成:

row.xml包含

ImageView 
TextView 
TextView 
TextView 
CheckBox 

在我的活動類:

Uri uriSms = Uri.parse("content://sms/sent"); 
Cursor cursor = context.getContentResolver().query(uriSms, null,null,null,null); 
String[] from={"address","body"}; 
int[] to={R.id.contactName,R.id.msgLine}; 
ssa=new SentSmsAdapter(context,R.layout.inbox_list_item,cursor,from,to,2); 
smsList.setAdapter(ssa); 

deleteSms.setOnClickListener(new OnClickListener(){ 

      @Override 
      public void onClick(View arg0) { 
       // TODO Auto-generated method stub 

       ArrayList<Boolean> list=SentSmsAdapter.itemChecked; 

       Uri path=Uri.parse("content://sms/"); 
       for(int i=0;i<list.size();i++) 
       { 
        if(list.get(i)) 
         getContentResolver().delete(path,"_id="+ssa.getItemId(i),null); 
       } 

       ssa.notifyDataSetChanged(); 
} 

我有自定義SimpleCursorAdapter。

public class SentSmsAdapter extends SimpleCursorAdapter{ 

    Cursor dataCursor; 
    LayoutInflater mInflater; 
    Context context; 
    int layoutType; 
    ArrayList<String> arrayList; 
    public static HashMap<String,Long> myList=new HashMap<String,Long>(); 

    public static ArrayList<Boolean> itemChecked = new ArrayList<Boolean>(); 
    public static ArrayList<Long> itemIds=new ArrayList<Long>(); 

    public SentSmsAdapter(Context context, int layout, Cursor dataCursor, String[] from, 
        int[] to,int type) { 
      super(context, layout, dataCursor, from, to); 


      layoutType=type; 
      this.context=context; 
      this.dataCursor = dataCursor; 
    mInflater = LayoutInflater.from(context); 

    arrayList=new ArrayList<String>(); 

    for (int i = 0; i < this.getCount(); i++) { 
     itemChecked.add(i, false); 
    } 

    } 

    @Override 
    public View getView(final int position, View convertView, ViewGroup parent) { 

      final ViewHolder holder; 

      if(convertView==null) 
      { 
        convertView = mInflater.inflate(R.layout.inbox_list_item, null); 
      holder = new ViewHolder(); 

      holder.checkBox=(CheckBox) convertView.findViewById(R.id.checkMsg); 
      holder.checkBox.setTag(position); 
      holder.cName=(TextView)convertView.findViewById(R.id.contactName); 
      holder.icon=(ImageView)convertView.findViewById(R.id.msgImage); 
      holder.msg=(TextView)convertView.findViewById(R.id.msgLine); 
      holder.time=(TextView)convertView.findViewById(R.id.msgTime); 



      convertView.setTag(holder); 
      holder.checkBox.setTag(itemChecked.get(position)); 
      } 
      else 
      { 
        holder=(ViewHolder)convertView.getTag(); 
      } 

       holder.checkBox.setOnClickListener(new OnClickListener(){ 


            @Override 
            public void onClick(View view) { 
              // TODO Auto-generated method stub 
              CheckBox cb = (CheckBox) view.findViewById(R.id.checkMsg); 
              int pos=Integer.parseInt(holder.checkBox.getTag()); 

            itemChecked.set(position, cb.isChecked()); 

              /*if (cb.isChecked()) { 
            itemChecked.set(position, true); 

            Log.i("WhenChecked",Boolean.toString(itemChecked.get(position))); 
            // do some operations here 
           } 
              else if (!cb.isChecked()) { 
            itemChecked.set(position, false); 
            Log.i("WhenNotChecked",Boolean.toString(itemChecked.get(position))); 
            // do some operations here 
           } 
            */ 

            } 
          }); 

      dataCursor.moveToPosition(position); 

      String id=Integer.toString(dataCursor.getInt(dataCursor.getColumnIndexOrThrow("_id"))); 
      itemIds.add(Long.parseLong(id)); 

      String msgText=dataCursor.getString(dataCursor.getColumnIndexOrThrow("body")); 
      holder.msg.setText(msgText); 

      Long time=dataCursor.getLong(dataCursor.getColumnIndexOrThrow("date")); 
      holder.time.setText(Long.toString(time)); 

      String address=dataCursor.getString(dataCursor.getColumnIndexOrThrow("address")); 

      Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(address));   
      Cursor cs= context.getContentResolver().query(uri, new String[]{PhoneLookup.DISPLAY_NAME},null,null,null); 

      if(cs.getCount()>0) 
        address=cs.getString(cs.getColumnIndex(PhoneLookup.DISPLAY_NAME)); 

      cs.close(); 
      holder.cName.setText(address); 


      if(layoutType==1)holder.checkBox.setVisibility(View.GONE); 
      else 
      { 
        holder.checkBox.setVisibility(View.VISIBLE); 
      } 
      arrayList.add(id+","+address+","+msgText+","+Long.toString(time)); 
      return convertView; 
    } 

    static class ViewHolder 
    { 
      ImageView icon; 
      CheckBox checkBox; 
      TextView cName; 
      TextView msg; 
      TextView time; 
    } 

    @Override 
    public Object getItem(int position) { 
      // TODO Auto-generated method stub 
      return arrayList.get(position); 
    } 

    @Override 
    public long getItemId(int position) { 
      // TODO Auto-generated method stub 
      return itemIds.get(position); 
    } 

}

回答

4

嘗試這樣的事情。

加入getView()此代碼

holder.checkBox.seTag(position); 
holder.checkBox.setOnCheckedChangedListener(this); 

實現此外getView()。

public void onCheckedChanged(CompoundButton view,boolean isChecked) { 

    if(isChecked) 
    { 
     itemChecked.add(view.getTag());       
    } 
    else 
    { 
     if(itemChecked.cantains(view.getTag())) 
      //remove from itemChecked. 
    } 
} 

刪除時,刪除列表中索引在itemChecked中可用的所有元素。

感謝和N-JOY。

+0

我有一個類似於Kartik的類,但我使用的是ArrayAdapter。當單擊其中一個複選框時,將爲所有複選框調用onCheckChanged事件。我使用ArrayAdapter的事實不應該改變這個權利? –

+1

我用onClick代替onCheckedChanged就像我在這裏閱讀(http://stackoverflow.com/questions/5438375/custom-listview-with-checkbox-problem),我得到它的工作。 –

0

如果您檢查,uncheking和刪除您的項目時使用線程,它們將正常工作。

相關問題