title: Custom ListView with CheckBox
body: <p>I know there are lots of questions related to Custom ListView with CheckBox but still i am getting some problem in retrieving all checked items.</p>
  
  <p>What i am doing is :</p>
  
  <p>Displaying Custom ListView from Database (List of All Sent SMSs).</p>
  
  <p>Allow user to check various list items.</p>
  
  <p>When user presses the Delete Button i want to delete all the checked items (From the database as well as the view portion)</p>
  
  <p><strong>Problem :</strong>
  When i go to my Activity for the first time and check some items, and delete it, it works fine.</p>
  
  <p>But when i again check some items just after pressing delete button, some items gets checked and some gets unchecked and again some other items are deleted..</p>
  
  <p>I think i am not able to bind id and list item perfectly..</p>
  
  <p><strong>Coding done so far</strong>:</p>
  
  <p>row.xml contains</p>
  
  <pre><code>ImageView
  TextView
  TextView
  TextView
  CheckBox 
  </code></pre>
  
  <p>In my Activity Class :</p>
  
  <pre><code>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&lt;Boolean&gt; list=SentSmsAdapter.itemChecked;
  
                  Uri path=Uri.parse("content://sms/");
                  for(int i=0;i&lt;list.size();i++)
                  {
                      if(list.get(i))
                          getContentResolver().delete(path,"_id="+ssa.getItemId(i),null);
                  }
  
                  ssa.notifyDataSetChanged();
  }
  </code></pre>
  
  <p>I have custom SimpleCursorAdapter.</p>
  
  <pre><code> public class SentSmsAdapter extends SimpleCursorAdapter{
  
      Cursor dataCursor;
      LayoutInflater mInflater;
      Context context;
      int layoutType;
      ArrayList&lt;String&gt; arrayList;
      public static HashMap&lt;String,Long&gt; myList=new HashMap&lt;String,Long&gt;();
  
      public static ArrayList&lt;Boolean&gt; itemChecked = new ArrayList&lt;Boolean&gt;();
      public static ArrayList&lt;Long&gt; itemIds=new ArrayList&lt;Long&gt;();
  
      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&lt;String&gt;();
  
      for (int i = 0; i &lt; 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()&gt;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);
      }
  </code></pre>
  
  <p>}</p>