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

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

I removed final keyword from "final int agr2" but it shows error at db.deleteData(arg2+""); and userList.remove(arg2);
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.
Does it say anything when you hover over the "arg2"?
Yes, it says("Variable 'arg2' is accessed from within inner class, needs to be declared final")
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: stackoverflow.com/questions/2808501/…. Put your deletion code in a method in the outer class and call that method from the inner class when clicked.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.