Skip to content Skip to sidebar Skip to footer

Delete Query And Refresh In Listview In Android (sqlite)

I'm new to Java, Android, and SQLite, and I'm stuck at this. I have columns, namely _id (autoincrement), type, amount (int), category, and description. Three queries. Firstly, I am

Solution 1:

Firstly, I am not able to delete a transaction (row) retrieved from database

Line 61 is casting the result of arg0.getItemAtPosition(arg2) to Cursor, but the return type is probably HashMap (as it says in the Logcat output). Normally you get a Cursor from a database query.

If you put the ID for each database row into the HashMap when you're building it, then you'll be able to pass that ID to your deleteTransaction() call in your onClick event. So, you need

temp.put("Id", localCursor.getInt(localCursor.getColumnIndex("_id")));

in getAllTransaction(), and then revise your onClick() method to do something like this:

localDbCrud.open();
HashMap itemMap = (HashMap)localAdapter.getItem(arg2);
int item_id = Integer.parseInt((String)itemMap.get("Id"));
DbCrud.deleteTransaction(item_id);
localDbCrud.close();

I would also suggest renaming arg2 (and others) to have clearer names so that the code is easier to follow.

Secondly, I need to know how to refresh the listview after deleting the entry

You can call notifyDataSetChanged() on your adapter to refresh the ListView after you've made a change to the dataset.

EDIT: Please note that SimpleAdapter is meant for static data, so your mileage may vary. The best solution is probably to switch to a different type of adapter, such as ArrayAdapter, or make a new SimpleAdapter every time you change the dataset.

Post a Comment for "Delete Query And Refresh In Listview In Android (sqlite)"