title: How to delete data from list-view which is checked
body: <p>I have custom list-view which loads data from external database <code>MySQL</code>. Now i have a problem while deleting data, when i check any particular check-box and try to delete data it delete's all the data in database not the selected one, but i just want to delete checked records. </p>
  
  <p>I know why this problem is occurring but i don't know how to solve this, problem is that while deleting it is not getting checked value position because i have not done any coding regarding checking particular position and delete. Because i don't know how can i get the checked value.</p>
  
  <p>So anyone please tell me how to delete only checked values... Below is my full code. FOr information i have used <a href="http://www.androidhive.info/2012/05/how-to-connect-android-with-php-mysql/" rel="nofollow">http://www.androidhive.info/2012/05/how-to-connect-android-with-php-mysql/</a> this as a reference. </p>
  
  <pre><code>public class Classes_Ext_DB extends Activity implements OnClickListener {
  
  ImageView imageViewNewClass, imageViewDelete;
  ListView mListView;
  
  /** Sliding Menu */
  boolean alreadyShowing = false;
  private int windowWidth;
  private Animation animationClasses;
  private RelativeLayout classesSlider;
  LayoutInflater layoutInflaterClasses;
  ArrayAdapter&lt;CharSequence&gt; adapterSpinner;
  
  private ProgressDialog pDialog;
  // Creating JSON Parser object
  JSONParser jParser = new JSONParser();
  
  ArrayList&lt;HashMap&lt;String, String&gt;&gt; productsList;
  
  // url to get all products list
  private static String url_all_classDetail = "http://my-server/get_all_classdetail.php";
  
  // url to delete product
  private static final String url_delete_class = "http://my-server/delete_class.php";
  
  // JSON Node names
  private static final String TAG_SUCCESS = "success";
  private static final String TAG_PRODUCTS = "products";
  private static final String TAG_ID = "ID";
  private static final String TAG_CLASSNAME = "class_name";
  
  // products JSONArray
  JSONArray products = null;
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.classes);
  
      imageViewNewClass = (ImageView) findViewById(R.id.newclass);
      imageViewDelete = (ImageView) findViewById(R.id.deletemenu);
      mListView = (ListView) findViewById(R.id.displaydata);
  
      productsList = new ArrayList&lt;HashMap&lt;String, String&gt;&gt;();
  
      imageViewNewClass.setOnClickListener(new View.OnClickListener() {
          public void onClick(View v) {
              Intent intent = new Intent(Classes_Ext_DB.this,
                      Class_Create_Ext_DB.class);
              startActivity(intent);
          }
      });
  
      imageViewDelete.setOnClickListener(new OnClickListener() {
          @Override
          public void onClick(View arg0) {
              AlertDialog.Builder alertDialog = new AlertDialog.Builder(
                      Classes_Ext_DB.this);
  
              final Spinner spinnerDelete = new Spinner(Classes_Ext_DB.this);
              alertDialog.setView(spinnerDelete);
  
              adapterSpinner = ArrayAdapter.createFromResource(
                      Classes_Ext_DB.this, R.array.delete_menu,
                      android.R.layout.simple_spinner_item);
              adapterSpinner
                      .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
              spinnerDelete.setAdapter(adapterSpinner);
  
              alertDialog.setPositiveButton("Ok",
                      new DialogInterface.OnClickListener() {
                          public void onClick(DialogInterface dialog,
                                  int which) {
                              if (spinnerDelete.getSelectedItemPosition() == 0) {
                                  new DeleteProduct().execute();
                              }
                          }
                      });
              alertDialog.setNegativeButton("Cancel",
                      new DialogInterface.OnClickListener() {
                          public void onClick(DialogInterface dialog,
                                  int which) {
                              dialog.cancel();
                          }
                      });
              alertDialog.show();
  
          }
      });
  
      new LoadAllClassDetail().execute();
  }
  
  class LoadAllClassDetail extends AsyncTask&lt;String, String, String&gt; {
  
      /**
       * Before starting background thread Show Progress Dialog
       * */
      @Override
      protected void onPreExecute() {
          super.onPreExecute();
          pDialog = new ProgressDialog(Classes_Ext_DB.this);
          pDialog.setMessage("Loading Classes. Please wait...");
          pDialog.setIndeterminate(false);
          pDialog.setCancelable(false);
          pDialog.show();
      }
  
      /**
       * getting All products from url
       * */
      protected String doInBackground(String... args) {
          // Building Parameters
          List&lt;NameValuePair&gt; params = new ArrayList&lt;NameValuePair&gt;();
          // getting JSON string from URL
          JSONObject json = jParser.makeHttpRequest(url_all_classDetail,
                  "GET", params);
  
          // Check your log cat for JSON reponse
          Log.d("All Classes: ", json.toString());
  
          try {
              // Checking for SUCCESS TAG
              int success = json.getInt(TAG_SUCCESS);
  
              if (success == 1) {
                  // products found
                  // Getting Array of Products
                  products = json.getJSONArray(TAG_PRODUCTS);
  
                  // looping through All Products
                  for (int i = 0; i &lt; products.length(); i++) {
                      JSONObject c = products.getJSONObject(i);
  
                      // Storing each json item in variable
                      String cid = c.getString(TAG_ID);
                      String cn = c.getString(TAG_CLASSNAME);
  
                      // creating new HashMap
                      HashMap&lt;String, String&gt; map = new HashMap&lt;String, String&gt;();
  
                      // adding each child node to HashMap key =&gt; value
                      map.put(TAG_ID, cid);
                      map.put(TAG_CLASSNAME, cn);
  
                      // adding HashList to ArrayList
                      productsList.add(map);
                  }
              } else {
                  // no products found
                  // Launch Add New product Activity
                  Intent i = new Intent(getApplicationContext(),
                          Class_Create_Ext_DB.class);
  
                  // Closing all previous activities
                  i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                  startActivity(i);
              }
          } catch (JSONException e) {
              e.printStackTrace();
          }
  
          return null;
      }
  
      /**
       * After completing background task Dismiss the progress dialog
       * **/
      protected void onPostExecute(String file_url) {
  
          // dismiss the dialog after getting all products
          pDialog.dismiss();
  
          // updating UI from Background Thread
          runOnUiThread(new Runnable() {
              public void run() {
  
                  /**
                   * Updating parsed JSON data into ListView
                   * */
                  ListAdapter adapter = new SimpleAdapter(
                          Classes_Ext_DB.this, productsList,
                          R.layout.custom_class, new String[] {
                                  TAG_CLASSNAME, TAG_ID },
                          new int[] { R.id.classname});
  
                  // updating listview
                  mListView.setAdapter(adapter);
                  mListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
                  mListView.setCacheColorHint(Color.TRANSPARENT);
                  mListView.setDivider(new ColorDrawable(0x99000000));
                  mListView.setDividerHeight(1);
              }
          });
      }
  }
  
  /*****************************************************************
   * Background Async Task to Delete Class
   * */
  class DeleteProduct extends AsyncTask&lt;String, String, String&gt; {
  
      /**
       * Before starting background thread Show Progress Dialog
       * */
  
      @Override
      protected void onPreExecute() {
          super.onPreExecute();
          pDialog = new ProgressDialog(Classes_Ext_DB.this);
          pDialog.setMessage("Deleting Class... Please wait...");
          pDialog.setIndeterminate(false);
          pDialog.setCancelable(true);
          pDialog.show();
      }
  
      /**
       * Deleting product
       * */
      protected String doInBackground(String... args) {
  
          // Check for success tag
          int success;
          try {
              // Building Parameters
              List&lt;NameValuePair&gt; params = new ArrayList&lt;NameValuePair&gt;();
              params.add(new BasicNameValuePair("ID", TAG_ID));
  
              // getting product details by making HTTP request
              JSONObject json = jParser.makeHttpRequest(url_delete_class,
                      "POST", params);
              // check your log for json response
              Log.d("Delete Product", json.toString());
              // json success tag
              success = json.getInt(TAG_SUCCESS);
              if (success == 1) {
              }
          } catch (JSONException e) {
              e.printStackTrace();
          }
          return null;
      }
  
      /**
       * After completing background task Dismiss the progress dialog
       * **/
      protected void onPostExecute(String file_url) {
          pDialog.dismiss();
      }
  }
  }
  </code></pre>
  
  <p><strong>custom_class.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  </code></pre>
  
  <p></p>
  
  <pre><code>&lt;CheckBox
      android:id="@+id/checkBox1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content" /&gt;
  
  &lt;TextView
      android:id="@+id/classname"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginLeft="10dip"
      android:textAppearance="?android:attr/textAppearanceMedium"
      android:textColor="#000000"
      android:textStyle="bold" /&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>Now the problem is that i have caled my custom class like   </p>
  
  <pre><code>                       ListAdapter adapter = new SimpleAdapter(
                          Classes_Ext_DB.this, productsList,
                          R.layout.custom_class, new String[] {
                                  TAG_CLASSNAME, TAG_ID },
                          new int[] { R.id.classname});
  </code></pre>
  
  <p>So i don't how can i declare my <code>Check-box</code> and get the particular <code>check-box</code> value for deleting or editing..</p>
  
  <p>Please help me to sort out this issue. Thank you in advance. Any help would be greatly appreciated. </p>
  
  <p>** EDIT **</p>
  
  <p><code>delete_class.php</code></p>
  
  <pre><code>&lt;?php
  
  /*
   * Following code will delete a product from table
   * A product is identified by product id (pid)
   */
  
  // array for JSON response
  $response = array();
  
  // check for required fields
  if (isset($_POST['ID'])) {
      $ID= $_POST['ID'];
  
      // include db connect class
      require_once ('db_connect.php');
  
      // connecting to db
      $db = new DB_CONNECT();
  
      // mysql update row with matched pid
      $result = mysql_query("DELETE FROM class_create WHERE ID = $ID");
  
      // check if row deleted or not
      if (mysql_affected_rows() &gt; 0) {
          // successfully updated
          $response["success"] = 1;
          $response["message"] = "Product successfully deleted";
  
          // echoing JSON response
          echo json_encode($response);
      } else {
          // no product found
          $response["success"] = 0;
          $response["message"] = "No product found";
  
          // echo no users JSON
          echo json_encode($response);
      }
  } else {
      // required field is missing
      $response["success"] = 0;
      $response["message"] = "Required field(s) is missing";
  
      // echoing JSON response
      echo json_encode($response);
  }
  ?&gt;
  </code></pre>
  
  <p><code>get_all_classdetail.php</code></p>
  
  <pre><code>&lt;?php
  
  /*
   * Following code will list all the products
   */
  
  // array for JSON response
  $response = array();
  
  
  // include db connect class
  require_once ('db_connect.php');
  
  // connecting to db
  $db = new DB_CONNECT();
  
  // get all products from products table
  $result = mysql_query("SELECT * FROM class_create") or die(mysql_error());
  
  // check for empty result
  if (mysql_num_rows($result) &gt; 0) {
      // looping through all results
      // products node
      $response["products"] = array();
  
      while ($row = mysql_fetch_array($result)) {
          // temp user array
          $product = array();
          $product["ID"] = $row["ID"];
          $product["class_name"] = $row["class_name"];
          $product["class_days"] = $row["class_days"];
          $product["class_time"] = $row["class_time"];
  
  
          // push single product into final response array
          array_push($response["products"], $product);
      }
      // success
      $response["success"] = 1;
  
      // echoing JSON response
      echo json_encode($response);
  } else {
      // no products found
      $response["success"] = 0;
      $response["message"] = "No products found";
  
      // echo no users JSON
      echo json_encode($response);
  }
  ?&gt;
  </code></pre>