0

I'm trying to delete the item from the database by longPress on the listview. When I click delete the item is removed from the listview and shows a Toast message deleted but if I moves to other Activity and returns back the item still exits in my listview. How to remove the item from database

Here, this is my code

 listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
                @Override
                        public boolean onItemLongClick(final AdapterView<?> arg0, View arg1, final int arg2, final long arg3) {
                            final AlertDialog.Builder delete = new AlertDialog.Builder(AddCount.this);
                            delete.setIcon(R.drawable.ic_baseline_delete_24);
                            delete.setTitle("Are you sure");
                            delete.setMessage("Do you want to delete this item?");
                            delete.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                                        public void onClick(DialogInterface dialog, int whichButton) {
                                            DbHandler db = new DbHandler(getApplicationContext());
                                                db.deleteData(arg2 + "");
                                                userList.remove(arg2);
                                                adapter.notifyDataSetChanged();
                                                Toast.makeText(getApplicationContext(), "Deleted", Toast.LENGTH_SHORT).show();
    
                                        }
                                    });
                            delete.setNegativeButton("No", new DialogInterface.OnClickListener() {
                                        public void onClick(DialogInterface dialog, int whichButton) {
                                            dialog.cancel();
                                        }
                                    });
                            delete.show();
                            return true;
                        }
                    });

Dbhandler.java(SQLite database)

public void deleteData(String id)
    {
        SQLiteDatabase db = this.getWritableDatabase();
        db.delete(TABLE_Inputs, "id = ? ", new String[]{id});
        db.close();
        }

This is how it shows after removing final keyword enter image description here

Learner
  • 41
  • 6

1 Answers1

1

https://developer.android.com/reference/android/widget/AdapterView.OnItemLongClickListener

In this link, they defined the method as follows: onItemLongClick(AdapterView<?> parent, View view, int position, long id).

Your definition: public boolean onItemLongClick(final AdapterView<?> arg0, View arg1, final int arg2, final long arg3).

You used "final int arg2". Since parameters defined with final keyword can't be reassigned, this may cause a problem. Try defining your method without final keyword and see if it works.

Teohan Eksi
  • 33
  • 1
  • 8
  • I removed final keyword from "final int agr2" but it shows error at db.deleteData(arg2+""); and userList.remove(arg2); – Learner Nov 04 '20 at 12:26
  • I don't know exactly what's happening rn. I'm going to dig deeper and see if i can come up with an answer. – Teohan Eksi Nov 04 '20 at 12:30
  • Does it say anything when you hover over the "arg2"? – Teohan Eksi Nov 04 '20 at 12:52
  • Yes, it says("Variable 'arg2' is accessed from within inner class, needs to be declared final") – Learner Nov 04 '20 at 13:02
  • If it's declared final, it can't be changed. So, i suggest to create a function in outer class and call it from the inner class. This is already answered here: https://stackoverflow.com/questions/2808501/calling-outer-class-function-from-inner-class. Put your deletion code in a method in the outer class and call that method from the inner class when clicked. – Teohan Eksi Nov 04 '20 at 15:32