早上好,我是Android开发的新手,我练习CRUD操作,但是在删除列表视图中的所选项目时遇到错误,我想在删除后更新列表视图。
这是我要在Database Helper中删除的代码
public void deletegroce(int groceId){
String groceid[] = {String.valueOf(groceId)};
SQLiteDatabase sqdb = this.getWritableDatabase();
sqdb.delete(TABLE_NAME, KEY_GROCE_ID + " = ?", groceid);
sqdb.close();
}
}
这是我的清单代码
public class Grocery_list extends Activity {
ListView lvGorc;
DatabaseHelper dbhelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_grocery_list);
lvGorc = (ListView) findViewById(R.id.lvGroc);
dbhelper = new DatabaseHelper(Grocery_list.this);
final ArrayList<String> aList = dbhelper.getAllGroceries();
ArrayAdapter<String> La = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, aList);
lvGorc.setAdapter(La);
lvGorc.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(final AdapterView<?> adapterView, View view, int i, long l) {
AlertDialog.Builder adb = new AlertDialog.Builder(Grocery_list.this);
adb.setTitle("Option");
adb.setMessage("What do you want to do?");
adb.setPositiveButton("Update", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
adb.setNegativeButton("Delete", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dbhelper.deletegroce(i);
}
});
adb.show();
}
});
La.notifyDataSetChanged();
lvGorc.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
AlertDialog.Builder adb = new AlertDialog.Builder(Grocery_list.this);
adb.setTitle("Delete?");
adb.setMessage(aList.get(position));
adb.setPositiveButton("Delete", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dbhelper.deletegroce(aList.get(position));
}
});
adb.show();
return true;
}
});
}
}
非常感谢
答案 0 :(得分:0)
您的deleteGroce()方法需要一个int(groceId),但是您的ArrayAdapter get()方法将返回一个String值,您需要使用Int.parseInt(aList.get(position))而不是aList.get(位置)在调用方法中:)