Roshan George
Roshan George

Reputation: 187

How to delete an item from Sqlite database related to listview

I have a listview that populates data from an sqlite db. When I click on an item in the listview, it goes to a new page that shows the listview item in detail. In that page there is a delete button, which on clicking removes the item from the listview and also from the database.

How is it possible to do that?

Upvotes: 0

Views: 422

Answers (2)

SBotirov
SBotirov

Reputation: 14138

Solutions:

1. First solution:

When user click listView item you send next activity item position which you showing item detail activity, and if user pressed delete and when user back listView activity you send to back this position to listView Activity. And here check if position yes than need remove this item and refresh listView:

    listViewAdapter.remove(listViewAdapter.getItem(position));
    listViewAdapter.notifyDataSetChanged();

For send data to detail Activity and for give data from detail
Activity use onActivityResult method, see more information:

Getting a Result from an Activity

2. Second solution:

Create Application class extend Application and use it is. Create

int selectedItemPosition
boolean itemDeleted

field in the Application class. When user click item set this item position to this field and set itemDeleted value false. When user click delete button in the Detail activity you need set itemDeleted value true and when user back listView activity always check itemDeleted value if true delete this item and refresh listView.

Upvotes: 0

bakriOnFire
bakriOnFire

Reputation: 2681

When you are populating the listview from sqlite, poulate an array with the record ids of the records from sqlite table. Pass the selected record id from that array to the listview detail page. Then in onClick of the delete button just delete the record from sqlite and repaint your listview from the database.

Upvotes: 1

Related Questions