当我尝试删除列表视图中的项目时,它会将其从列表视图中删除,但是当我添加新项目或再次运行应用程序时,项目仍然存在。我无法从数据库中删除它。
我正在使用此代码:
...
SqlHandler sqlHandler;
ListView myListView;
myAdapter adapter;
ArrayList<myItems> items;
...
myListView = (ListView) findViewById(R.id.myListView);
sqlHandler = new SqlHandler(this);
items = getItemsFromDatabase();
adapter = new myAdapter(this, items);
myListView.setAdapter(adapter);
myListView.setOnItemLongClickListener(this);
...
public boolean onItemLongClick(AdapterView<?> adapterView, View view,
int position, long id) {
final myItems selectedItem = items.get(position);
if (selectedItem != null) {
AlertDialog.Builder alt_bld = new AlertDialog.Builder(this);
alt_bld.setMessage("Do you want to delete this item?")
.setCancelable(false)
.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
sqlHandler=new SqlHandler(getApplicationContext());
sqlHandler.deleteRecord(id);
items.remove(selectedItem);
adapter.notifyDataSetChanged();
对于我使用的数据库:
public class SqlHandler {
public static final String DATABASE_NAME = "MY_DATABASE";
public static final String DATABASE_TABLE = "MEM";
public static final String KEY_ROWID = "_id";
public static final int DATABASE_VERSION = 1;
Context context;
SQLiteDatabase sqlDatabase;
SqlDbHelper dbHelper;
public SqlHandler(Context context) {
dbHelper = new SqlDbHelper(context, DATABASE_NAME, null,
DATABASE_VERSION);
sqlDatabase = dbHelper.getWritableDatabase();
}
public void executeQuery(String query) {
try {
if (sqlDatabase.isOpen()) {
sqlDatabase.close();
}
sqlDatabase = dbHelper.getWritableDatabase();
sqlDatabase.execSQL(query);
} catch (Exception e) {
System.out.println("DATABASE ERROR " + e);
}
}
public Cursor selectQuery(String query) {
Cursor c1 = null;
try {
if (sqlDatabase.isOpen()) {
sqlDatabase.close();
}
sqlDatabase = dbHelper.getWritableDatabase();
c1 = sqlDatabase.rawQuery(query, null);
} catch (Exception e) {
System.out.println("DATABASE ERROR " + e);
}
return c1;
}
public SqlHandler open() throws SQLException
{
sqlDatabase = dbHelper.getWritableDatabase();
return this;
}
public void Close(){
dbHelper.close();
}
public void deleteRecord(long rowId){
try {
sqlDatabase.delete(DATABASE_TABLE,KEY_ROWID + "="+rowId,null);
}
catch (Exception e)
{
Log.e("DB ERROR", e.toString());
e.printStackTrace();
}
}
和
public class SqlDbHelper extends SQLiteOpenHelper {
public static final String DATABASE_TABLE = "MEM";
public static final String TAG="DbHandler";
public static final String KEY_ROWID = "_id";
...
private static final String SCRIPT_CREATE_DATABASE = "create table "
+ DATABASE_TABLE + " (" + KEY_ROWID
+ " integer primary key autoincrement, " + ....);";
public SqlDbHelper(Context context, String name, CursorFactory factory,
int version) {
super(context, name, factory, version);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(SCRIPT_CREATE_DATABASE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
onCreate(db);
}
---------- UPDATE ---------------------
应该是这样的:
myItems theItems =(myItems)adapterView.getItemAtPosition(button_id);
String _id = theItems.getID();
String delQuery = "DELETE FROM MEM WHERE _id='"+_id+"' ";
sqlHandler.executeQuery(delQuery);
但我的(myItems)adapterView有问题。我不知道如何处理这个问题。
答案 0 :(得分:1)
我相信错误在这里:
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) { //<--- rename this to buttonId
sqlHandler=new SqlHandler(getApplicationContext());
sqlHandler.deleteRecord(id); //<---- This "id" is the button that was clicked, not your item ID
我想你想要使用其他 id
变量,但即便如此,我也不确定那是你真正想要的那个(可能是)。从您的代码中我无法快速分辨出真实ID的位置。
答案 1 :(得分:1)
你遇到的问题是调用你的方法删除,它应该是这样的:
public long deleteItem(myItems itemToDelete) {
try {
return sqlDatabase.delete(DATABASE_TABLE,COLUMN_ID + " = ?",new String[]{itemToDelete.getID()});
}
catch (Exception e)
{
Log.e("DB ERROR", e.toString());
e.printStackTrace();
return -1;
}
}
当您按YES
的{{1}} Button
确认删除时,您应该从数据库中删除该项目,然后从数据库重新加载项目并通知您列表的适配器以刷新它与新数据:
AlertDialog
该项目将从数据库中删除,myItems selectedItem = adapter.getItem(position);
long rows = sqlHandler.deleteItem(selectedItem);
if(rows>0) {
//get new items from database
adapter.setItems(getItemsFromDatabase());
adapter.notifyDataSetChanged();
}
将使用新数据刷新