0

I have have checked every problem here in relation to my own. The codes are totally different in the one I'm using IMHO. I have coded everything up to this point. I've read that in deleting the data in the database is just by passing the position, the problem is the auto-generated id in the database is not the same with the position being passed. All I'm trying to achieve is after deleting the item in list view, the item in the database will also be deleted.

The code from database in getting all the data:

public ArrayList<String> getData() {
    String[] columns = new String[] { KEY_ID, KEY_CLIENT_NAME,
            KEY_PRODUCT_NAME, KEY_SUPPLIER_PRICE, KEY_BS_PRICE, KEY_QTY,
            KEY_TOTAL_AMOUNT };
    Cursor c = sql.query(DB_TABLE, columns, null, null, null, null, null);

    ArrayList<String> result = new ArrayList<String>();

    int id = c.getColumnIndex(KEY_ID);
    int cName = c.getColumnIndex(KEY_CLIENT_NAME);
    int pName = c.getColumnIndex(KEY_PRODUCT_NAME);
    int suppPrice = c.getColumnIndex(KEY_SUPPLIER_PRICE);
    int bsPrice = c.getColumnIndex(KEY_BS_PRICE);
    int qty = c.getColumnIndex(KEY_QTY);
    int totalAmount = c.getColumnIndex(KEY_TOTAL_AMOUNT);

    for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {

        result.add("ID: " + c.getInt(id) + "\n" + "CLIENT NAME: "
                + c.getString(cName) + "\n" + "PRODUCT NAME: "
                + c.getString(pName) + "\n" + "SUPPLIER PRICE: "
                + c.getString(suppPrice) + "\n" + "RETAIL PRICE: "
                + c.getString(bsPrice) + "\n" + "QUANTITIY: "
                + c.getString(qty) + "\n" + "TOTAL: "
                + c.getString(totalAmount) + "\n");
    }

    return result;
}

The code in retrieving the data:

sql = new SQLDatabase(this);
    sql.open();
     ArrayList<String> data = sql.getData();
    sql.close();

The code the the deletion should be executed:

protected void onListItemClick(View view, int position, long id) {
    Log.i("TAG", "onListItemClick id=" + position + id);


      AlertDialog.Builder builder = new AlertDialog.Builder( Delete.this);

      builder.setMessage("WHAT DO YOU WANT TO DO?");
      builder.setNegativeButton("DELETE", new
      DialogInterface.OnClickListener() {

      @Override public void onClick(DialogInterface dialog, int which) { }
                   // DELETE CODE HERE
      });

      builder.setNeutralButton("CANCEL", new
      DialogInterface.OnClickListener() {

      @Override public void onClick(DialogInterface dialog, int which) { }
                // DO NOTHING
      });

      builder.setCancelable(false); AlertDialog alertdialog =
      builder.create(); alertdialog.show();

}

1 Answer 1

0

Add Tag to the column you inserted and use same tag value while delete operation in both list and DB.

4
  • Do you have a sample code on that? Sorry. I have no idea on how to do that.
    – droidH
    Mar 8, 2013 at 14:19
  • Add tag while adding each row in DB like ", item_id1, item_id2....". While preparing list view add this tag value to list item using mView.setTag("item_id1"). Now you can identify list item by using these assigned ids not by index. Mar 11, 2013 at 7:00
  • I did not achieve your suggestion and ended up in using simplecursoradapter. Thanks for the help. Sorry I can't vote it up. I need more reputation.
    – droidH
    Mar 11, 2013 at 7:47
  • 1
    If you solved the problem on your own you can post the solution as an answer and mark it as the answer. That way other people can benefit too.
    – Dwilson
    May 5, 2013 at 5:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.