0

I have a ListView with some items and in each item there is a ImageButton for deleting this item.

The method for deleting the item is inside an ArrayAdapter which uses a view holder.

            //ALERT DIALOG FOR DELETE
            AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
            builder.setTitle("Delete All?");
            builder.setNegativeButton("No",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,int which)
                        {
                            dialog.dismiss();

                        }
                    });
            builder.setPositiveButton("Yes",
                    new DialogInterface.OnClickListener()
                    {
                        public void onClick(DialogInterface dialog, int which)
                        {
                            mHelper.deleteAll(gmid,true);                   //DELETE ITEM
                            remove(getItem(position));                          //REMOVE ITEM FROM LIST
                            notifyDataSetChanged();
                            TextView txtGameCount = (TextView) ((Activity)getContext()).findViewById(R.id.txtGamesCount);
                            int GamesCount = mHelper.getGamesCount(tmid);       //GET COUNT OF ITEMS
                            txtGameCount.setText(String.valueOf(GamesCount));   //SET TEXT WITH NEW NUMBER OF ITEMS
                            dialog.dismiss();                                   //CLOSE DIALOG

                        }
                    });
            builder.show();

My problem is that the TextView for displaying the number of items is displaying "0" and not the actual count of items.

The count method is working while I am adding an item and it is the following:

public int getGamesCount(int id)
{
    String countQuery = "SELECT * FROM " + TABLEG +" WHERE t_id="+ id +"";
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(countQuery, null);
    return cursor.getCount();
}

What I am doing wrong here?

thank you

1 Answers1

0

You are using deleteAll() which will delete all records. To delete a particular record use this function.

 public void deleteRecords(String table, String whereClause,
                              String[] whereArgs) {
        db.delete(table, whereClause, whereArgs);

    }

Then there is no need to call

remove(getItem(position))

because

notifydatasetchange()

will reset the list again. You have called deleteAll() and the n you are finding count so that will return 0 only because all records are deleted.

Preetika Kaur
  • 1,991
  • 2
  • 16
  • 23