Used Arrayadapter to display elements on listview which are stored in a database. Now I want to delete items from both listview as well database. Please help me to solve it.
-
2Try [this](http://android-er.blogspot.in/2011/06/delete-row-in-sqlite-database.html) example – Praveenkumar Sep 10 '12 at 05:24
-
The sample provided by SpK is the one you needed – Shalini Sep 10 '12 at 05:30
-
delete from the database and use notifyChange on list – Gaurav Vashisth Sep 12 '12 at 04:46
3 Answers
To delete row from database try using the sample http://codinglookseasy.blogspot.in/2012/08/sqlite-database.html. and after doing the deletion from database delete the item from the arrayadapter and use adapter.notifyDataSetChanged() to delete it from the list view too

- 7,068
- 2
- 21
- 51
-
It helps me to delete items from DB but i dont want to delete all the rows from the table only when click on delete button the highlighted or first item on listview should delete.How can i do that? – Vilasmati Hanjagi Sep 10 '12 at 05:38
-
Consider using a Where clause in the delete statement like delete from tablename where columnind = theselected item – G_S Sep 10 '12 at 05:41
-
Consider using a Where clause in the delete statement like delete from tablename where columnind = theselected item – G_S Sep 10 '12 at 05:43
-
As you said i have done but it will delete complete listview itself rather i need to delete first item from listview.@Sharath G – Vilasmati Hanjagi Sep 10 '12 at 12:06
Consider using a cursor adapter to bind the list view to your database table. There's an example here : http://thinkandroid.wordpress.com/2010/01/09/simplecursoradapters-and-listviews/
The example uses the deprecated "startManagingCursor", which has been deprecated in favor of the CursorLoader. CursorLoader is nice once you finally get it implemented. You register some listeners and your UI is immediately updated to reflect any changes to the db (without your having to manually add/remove rows in the listview).
Check out What are the benefits of CursorLoaders?
-
I make use of ArrayAdapter for displaying on listview.Now i am able to delete item only when Db cleared and restart the activity that too it will work one time next time it wont delete only. – Vilasmati Hanjagi Sep 10 '12 at 08:33
-
How about using ArrayAdapter's "remove(T obj)" method? Then call ArrayAdapter's "notifyDataSetChanged()" to update the UI. – rposky Sep 10 '12 at 15:57
-
No ,even i tried for that also but no use because the data are stored in DB trying to delete on listview wont effect at all. – Vilasmati Hanjagi Sep 11 '12 at 03:31
-
Well, yes, you will still need to delete the entry in the database. @Sharath G has provided a link in his answer that can help you do just that. So, when you combine both of these approaches, you will have effectively removed the item from the database and listview. – rposky Sep 11 '12 at 04:23
-
Ya if i have deleted the data from DB means nothing is shown on listview .I wanted to do like click on delete button it should delete item from listview as well from DB.I did as @Sharath G provided link but it will delete all the data from DB not like as i needed.@rposky – Vilasmati Hanjagi Sep 11 '12 at 08:59
-
I want to delete data row by row from DB as well from listview .How can i do this?Please suggest me how can i do this. – Vilasmati Hanjagi Sep 11 '12 at 09:09
-
Ok, so you should have a unique key for all objects that you fetch from the database. Since you want the delete action to occur when a user "clicks" on the list view item delete button, I would store the unique object key with the delete button itself. So when inflating your list view, call "setTag([sqlite_row->key])" on the delete button. After adding your click event listener to the delete button (http://stackoverflow.com/questions/2468100/android-listview-click-howto), you will be able to retrieve the sqlite_row key by calling "getTag()" on the view that your listener receives. – rposky Sep 11 '12 at 15:17
-
Now that you have the primary key for the item that you wish to delete, you can remove it from your database following some of the suggestions here: http://stackoverflow.com/questions/7510219/deleting-row-in-sqlite-in-android To remove the item from your list view (after the item is successfully removed from the db), you'll need to determine the position of the list view item clicked. Once you have that position, you can call "getItem(position)" and finally, "remove(T object)" on your underlying array adapter. – rposky Sep 11 '12 at 15:27
-
I have pasted code bellow can you check it out and reply me?@rposky – Vilasmati Hanjagi Sep 12 '12 at 04:48
-
Even my requirement is delete should happen only when user clicks on delete button delete first item which is present on listview as well from DB.I should not do like on click on item to delete. – Vilasmati Hanjagi Sep 12 '12 at 04:54
Here is my code:
Cursor c = db.rawQuery("SELECT * FROM BOOKMARKS", null);
ID = c.getColumnIndex("ID");
audiotime=c.getColumnIndex("BookmarkTime");
// Check result.
c.moveToFirst();
if (c != null)
{
// Loop through all Results
do
{
/*String str=c.getString(ID);
audiobooks.add(str);*/
str=c.getString(audiotime);
audiobooks.add(str);
}
while(c.moveToNext());
c.close();
db.close();
//TODO
arrayAdapter=new ArrayAdapter<String>(this, R.layout.row3, R.id.itemr1,audiobooks);
lv.setAdapter(arrayAdapter);
cursor.close();
db.close(); //close db resources
}
public void onClick(View v)
{
if(v.getId()==R.id.Delete)
{
db = openOrCreateDatabase("DB", 0, null);
db.delete("BOOKMARKS", null, null);
int j=lv.getCount();
Toast.makeText(GetAudioBookmarks.this, " after delete listview count " +j,Toast.LENGTH_SHORT).show();
}
I could not able to do that because I am adding only bookmark string to the listview. I have pasted my code which will fetch data from DB and add to listview. Now my code will work like if I click on delete will delete all items on DB but listview count will toast as same value as it hold but wont display any data on listview.

- 5,886
- 3
- 29
- 38

- 20
- 9
-
All items are being deleted from your database table because you are not specifying a WHERE clause. You need to call the method like this: db.delete("BOOKMARKS", "ID=" + bookmarkid, null). What view is coming into your "OnClick" listener? This will likely determine how you get the bookmarkid, – rposky Sep 12 '12 at 06:22
-
-
I will get first item from listview while deleting but not deleting that item.I dont know what is the cause for it. – Vilasmati Hanjagi Sep 12 '12 at 06:41
-
Hey it's working for me now ,I removed the primary key from the table and it's working .Thanks for your help.@rposky – Vilasmati Hanjagi Sep 12 '12 at 06:53