首先,当我触摸上下文菜单'删除'时,我的情况没有错误,但有任何响应。它似乎对计算机保持不可读状态。
DBHelper.java的getAmount calss
public int getAmount(Integer id){
int amount = 0;
SQLiteDatabase db=this.getReadableDatabase();
Cursor res = db.rawQuery("SELECT id, amount FROM uselist WHERE id="+id,null);
amount=res.getInt(res.getColumnIndex(USELIST_COLUMN_AMOUNT));
return amount;
}
getAmount上面的代码已经使用了
@Override
public boolean onContextItemSelected(MenuItem item){
int itemId = item.getItemId();
switch (itemId){
case R.id.context_item1 :
Toast.makeText(this,"edit",Toast.LENGTH_SHORT).show();
return true;
case R.id.context_item2 :
int amt=-mydb.getAmount(position);
Toast.makeText(this,"delete",Toast.LENGTH_SHORT).show();
mydb.deleteUseList(position);
mydb.update("UPDATE uselist SET id = (id-1) WHERE id >"+ position+";");
mydb.update("UPATE uselist SET balance = (balance+"+amt+") WHERE id>"+position+";");
histories.clear(); //
finish();
startActivity(getIntent());
return true;
}
return super.onContextItemSelected(item);
}
我猜'int amt = -mydb.getAmount(position);'代码或getAmount类错误,因为删除Toast没有出现。
答案 0 :(得分:0)
试试这个:
public int getAmount(Integer id){
int amount = 0;
SQLiteDatabase db=this.getReadableDatabase();
Cursor res = db.rawQuery("SELECT id, amount FROM uselist WHERE id="+id,null);
// looping through all rows and setting value to variable
if (res.moveToFirst()) {
do {
amount=res.getInt(res.getColumnIndex(USELIST_COLUMN_AMOUNT));
} while (res.moveToNext());
}
return amount;
}