當我嘗試刪除列表視圖中的項目時,它將它從列表視圖中刪除,但是當我添加一個新項目項目或再次運行應用程序項目仍然存在。我無法從數據庫中刪除它。從列表視圖中刪除一個項目(使用sqlite)導致只從listview中刪除它。不能從數據庫中刪除它
我使用這個代碼:
...
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);
}
----------最新情況: --------------------
它應該是sth lik e:
myItems theItems =(myItems)adapterView.getItemAtPosition(button_id);
String _id = theItems.getID();
String delQuery = "DELETE FROM MEM WHERE _id='"+_id+"' ";
sqlHandler.executeQuery(delQuery);
但我有一個問題(myItems)adapterView ..我不知道如何處理這個。
擊敗我吧。做一些簡單的調試,你的適配器的「行ID」可能肯定與你的數據庫行ID有什麼不同(究竟是什麼取決於適配器)。 – 2013-04-11 13:38:25
@dmon:你好。那沒用。真正的ID? – George 2013-04-11 13:43:14
@dmon:我已更新。請檢查最後。謝謝 – George 2013-04-11 14:14:28