title: how to delete listview items
body: <p>Hi i want to check all listview items and to delete those items from database as well as from the listview.If all listview items are selected and any only one of the listitem is unchecked then the checked items have to be deleted.For this iam using menu's.How can i implement like this? Iam using the below code:</p>
  
  <pre><code>public void PopulateSentList() {
          String strquery = "SELECT * FROM sent_history";
          Cursor Cursor = (MainscreenActivity.JEEMAAndroSMSDB).rawQuery(strquery,
                  null);
          MyAdapter adapter = new MyAdapter(SentHistoryActivity.this, Cursor);
          setListAdapter(adapter);        
          lvhistory.setOnItemClickListener(new OnItemClickListener() {
  
              @Override
              public void onItemClick(AdapterView&lt;?&gt; arg0, View arg1,
                      int position, long arg3) {
                  // TODO Auto-generated method stub
                  SQLiteCursor selectedValue = (SQLiteCursor) getListAdapter()
                          .getItem(position);
                  String id1 = selectedValue.getString(0);
                  System.out.println("DATA--&gt;&gt;&gt;" + id1);
                  Intent intent = new Intent(getApplicationContext(),
                          Historydisplay.class);
                  intent.putExtra("Id", id1);
                  final int result = 1;
                  startActivityForResult(intent, result);
              }
          });
      }
  
      public void onBackPressed() {
          super.onBackPressed();
          Intent intent = new Intent(SentHistoryActivity.this,
                  MainscreenActivity.class);
          startActivity(intent);
          finish();
      }
  
      private void CreateMenu(Menu menu) {
          menu.setQwertyMode(true);
          MenuItem mnu1 = menu.add(0, 0, 0, "Delete");
          {
              mnu1.setAlphabeticShortcut('a');
          }
          MenuItem mnu2 = menu.add(1, 1, 1, "Select All");
          {
              mnu2.setAlphabeticShortcut('s');
          }
      }
  
      private boolean MenuChoice(MenuItem item) throws Exception {
          switch (item.getItemId()) {
          case 0:         
              if (getStrinValue != null) {
                  delhistory(getStrinValue);
              } else {
                  Toast.makeText(getApplicationContext(),
                          "Please select an Item", Toast.LENGTH_SHORT).show();
              }           
          case 1:
              if(item.getTitle().equals("Select All")){
                  for(int i=0; i &lt; lvhistory.getChildCount(); i++){
                      RelativeLayout itemLayout = (RelativeLayout)lvhistory.getChildAt(i);
                      final CheckBox cb = (CheckBox)itemLayout.findViewById(R.id.check);
                      cb.setChecked(true);           
                      if (cb.isChecked() == true) {                               
                          getStrinValue = getStrinValue + ","
                                  + cb.getTag().toString();
                      }
                      cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {                       
                          @Override
                          public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
                              // TODO Auto-generated method stub          
                              final long[] checkedIds = lvhistory.getCheckItemIds();
                              for (int i = 0; i &lt; checkedIds.length; i++) {
                                  Log.e("checkedIds", "id checked: " + checkedIds[i]);
                              }
                              }                       
                      });                 
                  }               
              }
  
          }
          return true;
      }
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          super.onCreateOptionsMenu(menu);
          CreateMenu(menu);
          return true;
      }
  
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
          try {
              return MenuChoice(item);
          } catch (Exception e) {
              e.printStackTrace();
          }
          return false;
      }
  
      private class MyAdapter extends ResourceCursorAdapter {
  
          public MyAdapter(Context context, Cursor cur) {
              super(context, R.layout.dummy, cur);
          }
  
          @Override
          public View newView(Context context, Cursor cur, ViewGroup parent) {
              LayoutInflater li = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
              return li.inflate(R.layout.dummy, parent, false);
          }
  
          @Override
          public void bindView(View view, Context context, Cursor cur) {
              TextView tvListText = (TextView) view.findViewById(R.id.Mobile);
              chkBox = (CheckBox) view.findViewById(R.id.check);          
              tvListText.setText(cur.getString(cur
                      .getColumnIndex(MainscreenActivity.COL_Mobile)));
              chkBox.setTag(cur.getString(cur
                      .getColumnIndex(MainscreenActivity.COL_Sent_id)));
  
              chkBox.setOnClickListener(new OnClickListener() {
                  @Override
                  public void onClick(View v) {
                      // TODO Auto-generated method stub
                      CheckBox cb = (CheckBox) v;
                      if (cb.isChecked() == true) {
                          getStrinValue = getStrinValue + ","
                                  + cb.getTag().toString();
                      } else {
                          getStrinValue = null;
                      }
                  }
              });
          }   
  
      }
  
  
      public void delhistory(String getStrinValue) {
          int pos1 = getStrinValue.indexOf(",");
          if (pos1 &gt; 0) {
              String rowId = getStrinValue.substring(pos1 + 1);
              String delimiter = "\\,";
              String[] sentID = rowId.split(delimiter);
              for (int i = 0; i &lt; sentID.length; i++) {
                  String temp0 = sentID[i];
                  int id = Integer.parseInt(temp0);
                  MainscreenActivity.JEEMAAndroSMSDB
                          .delete(MainscreenActivity.Table_SentHistory, "_id="
                                  + id, null);                
              }
              Toast.makeText(getApplicationContext(),
                      "History deleted successfully", Toast.LENGTH_SHORT)
                      .show();
              finish();
              Intent intent = new Intent(getApplicationContext(),
                      SentHistoryActivity.class);
              startActivity(intent);
          }
      }   
  </code></pre>