简体   繁体   English

如何从数据库以及Android的ListView中删除项目?

[英]How to delete an item from both database as well as listview Android?

Used Arrayadapter to display elements on listview which are stored in a database. 使用Arrayadapter在列表视图上显示存储在数据库中的元素。 Now I want to delete items from both listview as well database. 现在,我想同时从列表视图和数据库中删除项目。 Please help me to solve it. 请帮我解决。

To delete row from database try using the sample http://codinglookseasy.blogspot.in/2012/08/sqlite-database.html . 要从数据库中删除行,请尝试使用示例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 从数据库中删除后,从arrayadapter中删除该项目,并使用adapter.notifyDataSetChanged()从列表视图中删除它

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/ 这里有一个例子: 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. 该示例使用了不推荐使用的“ startManagingCursor”,为了支持CursorLoader,不推荐使用该参数。 CursorLoader is nice once you finally get it implemented. 一旦最终实现了CursorLoader,它就是很好的选择。 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). 您注册了一些侦听器,并且UI会立即更新以反映对数据库的任何更改(而无需在列表视图中手动添加/删除行)。

Check out What are the benefits of CursorLoaders? 看看CursorLoaders有什么好处?

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. 我粘贴了将从数据库获取数据并添加到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. 现在,我的代码将像单击删除按钮一样删除数据库上的所有项目,但listview的计数将保持不变,但不会在listview上显示任何数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM