Hishighness731
Hishighness731

Reputation: 130

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 not able to delete a transaction (row) retrieved from database. (Please refer to the attached Logcat).

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

And third, the silly issue I think. If I open the database once and retrieve data, it does so. Further, without closing the database, if I delete a transaction it does not delete it and gives the error "database not open". Further, if I close the database after retrieval and then open again at the time of deletion, it works. I dont get it. Main issue is the first one, but please answer if you know any of the above

    final ListView lv = getListView();
    localArrayList.clear();                //To Stop the silly repeating issue
    localDbCrud = new DbCrud(this);
    localAdapter = new SimpleAdapter(
            this,
            localArrayList,
            R.layout.transaction_list_item,
            new String[]{"Amount","Category","Date","Description"},
            new int[]{R.id.tli_amount,R.id.tli_category,R.id.tli_date,R.id.tli_desc});

    localDbCrud.open();
    DbCrud.getAllTransaction();             //Using HashMap localHashMap , localHashMap.put(localArrayList) in loop
    localDbCrud.close();
    lv.setOnItemLongClickListener(new OnItemLongClickListener() {

        public boolean onItemLongClick(final AdapterView<?> arg0, View arg1,
                 final int arg2, final long arg3) {
            // TODO Auto-generated method stub
            AlertDialog.Builder ladbuilder = new Builder(TransactionList.this);
            ladbuilder.setTitle("Choose Your Option");
            ladbuilder.setItems(R.array.alertdialog_prompt, new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int selected_item) {
                    // TODO Auto-generated method stub
                    if (selected_item == 0){
                        localDbCrud.open();
                        HashMap itemMap = (HashMap)localAdapter.getItem(arg2);
                        int item_id =(Integer) itemMap.get("Id");
                        DbCrud.deleteTransaction(item_id);
                        //final int ite=  (Integer) arg0.getItemAtPosition(arg2);
                         // final int item_id = c.getInt(c.getColumnIndex(DbCrud.TN_ID));
                        //DbCrud.deleteTransaction(item_id);
                        localDbCrud.close();

                    }
                    else{


                        //update code

                    }
                }
            });
            AlertDialog localad = ladbuilder.create();
            localad.show();


            return false;
        }
    });

    localDbCrud.close();

    setListAdapter(localAdapter);

}

Logcat

10-13 01:21:26.804: E/AndroidRuntime(22023): FATAL EXCEPTION: main
10-13 01:21:26.804: E/AndroidRuntime(22023): java.lang.ClassCastException: java.lang.String
10-13 01:21:26.804: E/AndroidRuntime(22023):    at com.hishighness.budgetracker.TransactionList$1$1.onClick(TransactionList.java:62)
10-13 01:21:26.804: E/AndroidRuntime(22023):    at com.android.internal.app.AlertController$AlertParams$3.onItemClick(AlertController.java:878)
10-13 01:21:26.804: E/AndroidRuntime(22023):    at android.widget.AdapterView.performItemClick(AdapterView.java:284)
10-13 01:21:26.804: E/AndroidRuntime(22023):    at android.widget.ListView.performItemClick(ListView.java:3701)
10-13 01:21:26.804: E/AndroidRuntime(22023):    at android.widget.AbsListView$PerformClick.run(AbsListView.java:1970)
10-13 01:21:26.804: E/AndroidRuntime(22023):    at android.os.Handler.handleCallback(Handler.java:587)
10-13 01:21:26.804: E/AndroidRuntime(22023):    at android.os.Handler.dispatchMessage(Handler.java:92)
10-13 01:21:26.804: E/AndroidRuntime(22023):    at android.os.Looper.loop(Looper.java:130)
    10-13 01:21:26.804: E/AndroidRuntime(22023):    at android.app.ActivityThread.main(ActivityThread.java:3687)
10-13 01:21:26.804: E/AndroidRuntime(22023):    at java.lang.reflect.Method.invokeNative(Native Method)
10-13 01:21:26.804: E/AndroidRuntime(22023):    at java.lang.reflect.Method.invoke(Method.java:507)
10-13 01:21:26.804: E/AndroidRuntime(22023):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
10-13 01:21:26.804: E/AndroidRuntime(22023):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
10-13 01:21:26.804: E/AndroidRuntime(22023):    at dalvik.system.NativeStart.main(Native Method)

here's the getallTransaction() function.This is the only trace of hash map in my project

    public static void getAllTransaction(){

    Cursor localCursor = localDatabase.query(true, TN_TABLE, null, null, null, null, null, null, null) ;
    if (localCursor != null) {

        localCursor.moveToFirst();
            do{
                HashMap<String,String> temp = new HashMap<String,String>();
                temp.put("Amount", localCursor.getString(localCursor.getColumnIndex("amount")));
                temp.put("Category", localCursor.getString(localCursor.getColumnIndex("category")));
                temp.put("Date", localCursor.getString(localCursor.getColumnIndex("date")));
                temp.put("Description", localCursor.getString(localCursor.getColumnIndex("description")));
                temp.put("Id", localCursor.getString(localCursor.getColumnIndex("_id")));
                TransactionList.localArrayList.add(temp);



            }while (localCursor.moveToNext());

    }

Upvotes: 1

Views: 2437

Answers (1)

acj
acj

Reputation: 4821

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.

Upvotes: 1

Related Questions