Tushar
Tushar

Reputation: 5935

delete a specific item from listview stored in database in android application

I am making an android application in which i have five items in a list .On each item click data is coming from url and then i have stored the items in local database.Now i want to have delete button before each item so that user can delete the item acc to his choice.That is how to delete a specific value from database in android application.I have tried googling ,,tried some ways given,but that didnot worked

Can anyone guide me how to accomplish this task?

Upvotes: 1

Views: 2880

Answers (2)

AD14
AD14

Reputation: 1218

you can set onclick listener for the items in the adapter and in that onclick you can delete the textview and using the above method can delete it from the database.

check this question and answer.

Deleting items from a ListView using a custom BaseAdapter

you can combine this and come to a solution. sorry i havent done something like you are doing. this is an idea.

Upvotes: 0

AD14
AD14

Reputation: 1218

you need to remove the item from the position and then set the adapter again so that the list will be refreshed.

listofitems.remove(tempPosition);

setListAdapter(customAdapter );

to remove from the database. use the position to get the id.

c.moveToPosition(position);
         id= c.getInt(c.getColumnIndex("_id"));

using the id you can delete

myDataBase.delete(tablename, "_id=?", new String[] {Integer.toString(id)});

Upvotes: 1

Related Questions