我的数据库类是这样的,当我调用deleteEntry
时,它不会删除0索引,这是数据库类方法
public int deleteEntry(String id)
{
SQLiteDatabase db = dbHelper.getWritableDatabase();
//String id=String.valueOf(ID);
String where="ID=?";
int numberOFEntriesDeleted= db.delete("TIME", where, new String[]{id}) ;
Toast.makeText(context, "Number of Entry Deleted Successfully : "+numberOFEntriesDeleted, Toast.LENGTH_LONG).show();
return numberOFEntriesDeleted;
}
这是我的onItemClickListener
list_view.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.d("Clicked item id", " " + id);
// data.remove(id);
String row = (String.valueOf(id));
data.deleteEntry(row);
答案 0 :(得分:0)
尝试传递位置而不是id:
list_view.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.d("Clicked item id", " " + id);
Log.d("Clicked item position", " " + position);
String row = (String.valueOf(position)); // the change is here
data.deleteEntry(row);
}
尝试此操作后,查看您获得的日志,并尝试确定哪个值(ID或位置)更好。无论如何,我想你应该存储你在某处显示的每个项目的数据库ID,这样你就不会只依赖列表中的位置(一旦从表中删除任何项目,这将无效),因为ID&# 39; s不会顺序。
答案 1 :(得分:0)
您可以尝试这样:
//---deletes a particular entry---
public int deleteEntry(String id)
{
SQLiteDatabase db = dbHelper.getWritableDatabase();
return db.delete("TIME", "ID = " + id, null) ;
}
顺便说一句,确保&#34; TIME&#34;是你的表名,你也关闭了数据库。
答案 2 :(得分:0)
这就是我所做的 考虑我有两个表,所以动态列表视图有两个项目(即使用自定义适配器)
我按名称删除项目(您可以在位置
获取所选项目的名称Yourlist.setOnItemLongClickListener(new OnItemLongClickListener(){@Override public boolean onItemLongClick(AdapterView arg0,View arg1,int arg2,long id){
String str = Yourlist.getItemAtPosition(arg2).toString();
//考虑我有两张桌子
//创建一个名为feedslist的表,其中包含一个名为“url”的列,其中将存储项目(feed url)
mydb.execSQL(“CREATE TABLE IF NOT EXISTS feedslist(id INTEGER PRIMARY KEY AUTOINCREMENT,url varchar);”);
//创建一个名为字幕列表的表,其中包含一个名为“name”的列,其中将存储项目(提要名称)
mydb.execSQL(“CREATE TABLE IF NOT EXISTS subtitleslist(id INTEGER PRIMARY KEY AUTOINCREMENT,name varchar);”);
//这是删除条目的方法 alert.setPositiveButton(R.string.deleteok,new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
//on positive click we delete the feed from selected position
//we're gonna delete them from the db
//using cursor
Cursor cursor =mydb.rawQuery("SELECT * FROM feedslist;", null);
Cursor cursor2 =mydb.rawQuery("SELECT * FROM subtitleslist;", null);
String url = "";
String name = "";
//set url
if (cursor != null && cursor.moveToFirst()) {
while (!cursor.isAfterLast()) {
//we get items at selected position
url = mItems.get(datposition);
cursor.moveToNext();
}
cursor.close();
}
//set feed name
if (cursor2 != null && cursor2.moveToFirst()) {
while (!cursor2.isAfterLast()) {
//we get items at selected position
name = mItems2.get(datposition);
cursor2.moveToNext();
}
cursor2.close();
}
//set the names of the two tables
String table1 = "feedslist";
String table2 = "subtitleslist";
//set where clause
String whereClause_url = "url" + "=?";
String whereClause_feed = "name" + "=?";
//set the where arguments
String[] whereArgs_url = new String[] { String.valueOf(url) };
String[] whereArgs_name = new String[] { String.valueOf(name) };
//delete 'em all
mydb.delete(table1, whereClause_url, whereArgs_url);
mydb.delete(table2, whereClause_feed, whereArgs_name);
//remove items from the dynamic listview
//for url
mItems.remove(datposition);
//for feed name
mItems2.remove(datposition);
//and update the dynamic list
//don't move this method above the db deletion method or
//you'll get javalangindexoutofboundsexception-invalid-index error
adapter_dynamic.notifyDataSetChanged();
adapter_dynamic.notifyDataSetInvalidated();
listfeed.setAdapter(adapter_dynamic);
您可以在此处查看工作应用