title: Using dynamically added checkbox to delete data from listview populated from database in android?
body: <p>Hi I am building an android app that uses checkbox to delete checked data from SQLite database. I write this code to populate data from SQlite database.</p>
  
  <pre><code>public class DisplayAdapter extends BaseAdapter {
      private Context mContext;
      private ArrayList&lt;String&gt; id;
      private ArrayList&lt;String&gt; firstName;
      private ArrayList&lt;String&gt; lastName;
      public static ArrayList&lt;Integer&gt; position=new ArrayList&lt;Integer&gt;();
  
  
      public DisplayAdapter(Context c, ArrayList&lt;String&gt; id,ArrayList&lt;String&gt; fname, ArrayList&lt;String&gt; lname) {
          this.mContext = c;
  
          this.id = id;
          this.firstName = fname;
          this.lastName = lname;
      }
  
      public int getCount() {
          // TODO Auto-generated method stub
          return id.size();
      }
  
      public Object getItem(int position) {
          // TODO Auto-generated method stub
          return null;
      }
  
      public long getItemId(int position) {
          // TODO Auto-generated method stub
          return 0;
      }
  
      public View getView(int pos, View child, ViewGroup parent) {
          final Holder mHolder;
          final int a=pos;
          LayoutInflater layoutInflater;
          if (child == null) {
              layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
              child = layoutInflater.inflate(R.layout.listcell, null);
              mHolder = new Holder();
              mHolder.txt_id = (TextView) child.findViewById(R.id.txt_id);
              mHolder.txt_fName = (TextView) child.findViewById(R.id.txt_fName);
              mHolder.txt_lName = (TextView) child.findViewById(R.id.txt_lName);
              mHolder.checkbox  = (CheckBox) child.findViewById(R.id.checkbox);
              mHolder.checkbox.setTag(a);
              position.add(a);
              mHolder.checkbox.setOnClickListener(new View.OnClickListener() {
  
                  public void onClick(View v) {
                      if(((CheckBox)v).isChecked()){
                          //Toast.makeText(mContext, ""+a,Toast.LENGTH_LONG).show();
                      }
                  }
              });
              child.setTag(mHolder);
  
          } else {
              mHolder = (Holder) child.getTag();
          }
          mHolder.txt_id.setText(id.get(pos));
          mHolder.txt_fName.setText(firstName.get(pos));
          mHolder.txt_lName.setText(lastName.get(pos));
  
          return child;
      }
  
      public class Holder {
          CheckBox checkbox;
          TextView txt_id;
          TextView txt_fName;
          TextView txt_lName;
      }
  
  }
  </code></pre>
  
  <p>I have a button (Delete) on the listView activity.After checking the checkboxes When a user presses delete button It will delete the data which was cecked from database and show the rest of the data remaining in Database.
  This is the activity where I am handling the click.</p>
  
  <pre><code>public class DisplayActivity extends Activity {
  
      private DbHelper mHelper;
      private SQLiteDatabase dataBase;
  
      private ArrayList&lt;String&gt; userId = new ArrayList&lt;String&gt;();
      private ArrayList&lt;String&gt; user_fName = new ArrayList&lt;String&gt;();
      private ArrayList&lt;String&gt; user_lName = new ArrayList&lt;String&gt;();
  
      private ListView userList;
      private AlertDialog.Builder build;
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.display_activity);
  
  
          userList = (ListView) findViewById(R.id.List);
  
          mHelper = new DbHelper(this);
          Button delete=(Button) findViewById(R.id.deleteBtn);
          delete.setOnClickListener(new View.OnClickListener() {
  
              public void onClick(View v) {
                  try {
                      if(userId.size()&gt;0){
  
                          for(int i=0;i&lt;DisplayAdapter.position.size();i++){
                              Toast.makeText(DisplayActivity.this, ""+i, Toast.LENGTH_SHORT).show();
                              View getView=userList.findViewWithTag(DisplayAdapter.position.get(i));
                              CheckBox checkbox = (CheckBox) getView
                                      .findViewById(R.id.checkbox);
                              if(checkbox.isChecked()){
                                  //Toast.makeText(DisplayActivity.this, ""+i, Toast.LENGTH_SHORT).show();
                                  dataBase.delete(
                                          DbHelper.TABLE_NAME,
                                          DbHelper.KEY_ID + "="
                                                  +userId.get(i), null);
  
                                  displayData();
                              }
  
                          }
  
                      }
                  } catch (Exception e) {
                      Toast.makeText(DisplayActivity.this, "Nothing Selected", Toast.LENGTH_SHORT).show();
                      e.printStackTrace();
                  }
  
              }
          });
          //add new record
          findViewById(R.id.btnAdd).setOnClickListener(new OnClickListener() {
  
              public void onClick(View v) {
  
                  Intent i = new Intent(getApplicationContext(),
                          AddActivity.class);
                  i.putExtra("update", false);
                  startActivity(i);
  
              }
          });
  
          //click to update data
          userList.setOnItemClickListener(new OnItemClickListener() {
  
              public void onItemClick(AdapterView&lt;?&gt; arg0, View arg1, int arg2,
                      long arg3) {
  
                  Intent i = new Intent(getApplicationContext(),
                          AddActivity.class);
                  i.putExtra("Fname", user_fName.get(arg2));
                  i.putExtra("Lname", user_lName.get(arg2));
                  i.putExtra("ID", userId.get(arg2));
                  i.putExtra("update", true);
                  startActivity(i);
  
              }
          });
      }
  
      @Override
      protected void onResume() {
          displayData();
          super.onResume();
      }
  
      /**
       * displays data from SQLite
       */
      private void displayData() {
          dataBase = mHelper.getWritableDatabase();
          Cursor mCursor = dataBase.rawQuery("SELECT * FROM "
                  + DbHelper.TABLE_NAME, null);
  
          userId.clear();
          user_fName.clear();
          user_lName.clear();
          if (mCursor.moveToFirst()) {
              do {
                  userId.add(mCursor.getString(mCursor.getColumnIndex(DbHelper.KEY_ID)));
                  user_fName.add(mCursor.getString(mCursor.getColumnIndex(DbHelper.KEY_FNAME)));
                  user_lName.add(mCursor.getString(mCursor.getColumnIndex(DbHelper.KEY_LNAME)));
  
              } while (mCursor.moveToNext());
          }
          DisplayAdapter disadpt = new DisplayAdapter(DisplayActivity.this,userId, user_fName, user_lName);
          userList.setAdapter(disadpt);
          mCursor.close();
  
      }
  
  
  
  }
  </code></pre>
  
  <p>It only deletes the first checked item from listview and as well databases.But i want to delete all the checked data from database.How do i do that? Details code and explanation would be appreciated. Thank in advance and sorry if i made any mistake.</p>