我是 Java、Android 和 SQLite 的新手,我被困在这个问题上。我有列,即_id
(autoincrement)、type
、amount
(int) category
、 和description
。三问。
首先,我无法删除从数据库中检索到的事务(行)。(请参考随附的 Logcat)。
其次,我需要知道删除条目后如何刷新 ListView。
第三,我认为的愚蠢问题。如果我打开数据库一次并检索数据,它就会这样做。此外,在不关闭数据库的情况下,如果我删除一个事务,它不会删除它并给出错误“数据库未打开”。此外,如果我在检索后关闭数据库,然后在删除时再次打开,它就可以工作。我不明白。主要问题是第一个,但如果您知道以上任何一个,请回答
final ListView lv = getListView();
localArrayList.clear(); //To Stop the silly repeating issue
localDbCrud = new DbCrud(this);
localAdapter = new SimpleAdapter(
this,
localArrayList,
R.layout.transaction_list_item,
new String[]{"Amount","Category","Date","Description"},
new int[]{R.id.tli_amount,R.id.tli_category,R.id.tli_date,R.id.tli_desc});
localDbCrud.open();
DbCrud.getAllTransaction(); //Using HashMap localHashMap , localHashMap.put(localArrayList) in loop
localDbCrud.close();
lv.setOnItemLongClickListener(new OnItemLongClickListener() {
public boolean onItemLongClick(final AdapterView<?> arg0, View arg1,
final int arg2, final long arg3) {
// TODO Auto-generated method stub
AlertDialog.Builder ladbuilder = new Builder(TransactionList.this);
ladbuilder.setTitle("Choose Your Option");
ladbuilder.setItems(R.array.alertdialog_prompt, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int selected_item) {
// TODO Auto-generated method stub
if (selected_item == 0){
localDbCrud.open();
HashMap itemMap = (HashMap)localAdapter.getItem(arg2);
int item_id =(Integer) itemMap.get("Id");
DbCrud.deleteTransaction(item_id);
//final int ite= (Integer) arg0.getItemAtPosition(arg2);
// final int item_id = c.getInt(c.getColumnIndex(DbCrud.TN_ID));
//DbCrud.deleteTransaction(item_id);
localDbCrud.close();
}
else{
//update code
}
}
});
AlertDialog localad = ladbuilder.create();
localad.show();
return false;
}
});
localDbCrud.close();
setListAdapter(localAdapter);
}
日志猫
10-13 01:21:26.804: E/AndroidRuntime(22023): FATAL EXCEPTION: main
10-13 01:21:26.804: E/AndroidRuntime(22023): java.lang.ClassCastException: java.lang.String
10-13 01:21:26.804: E/AndroidRuntime(22023): at com.hishighness.budgetracker.TransactionList$1$1.onClick(TransactionList.java:62)
10-13 01:21:26.804: E/AndroidRuntime(22023): at com.android.internal.app.AlertController$AlertParams$3.onItemClick(AlertController.java:878)
10-13 01:21:26.804: E/AndroidRuntime(22023): at android.widget.AdapterView.performItemClick(AdapterView.java:284)
10-13 01:21:26.804: E/AndroidRuntime(22023): at android.widget.ListView.performItemClick(ListView.java:3701)
10-13 01:21:26.804: E/AndroidRuntime(22023): at android.widget.AbsListView$PerformClick.run(AbsListView.java:1970)
10-13 01:21:26.804: E/AndroidRuntime(22023): at android.os.Handler.handleCallback(Handler.java:587)
10-13 01:21:26.804: E/AndroidRuntime(22023): at android.os.Handler.dispatchMessage(Handler.java:92)
10-13 01:21:26.804: E/AndroidRuntime(22023): at android.os.Looper.loop(Looper.java:130)
10-13 01:21:26.804: E/AndroidRuntime(22023): at android.app.ActivityThread.main(ActivityThread.java:3687)
10-13 01:21:26.804: E/AndroidRuntime(22023): at java.lang.reflect.Method.invokeNative(Native Method)
10-13 01:21:26.804: E/AndroidRuntime(22023): at java.lang.reflect.Method.invoke(Method.java:507)
10-13 01:21:26.804: E/AndroidRuntime(22023): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
10-13 01:21:26.804: E/AndroidRuntime(22023): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
10-13 01:21:26.804: E/AndroidRuntime(22023): at dalvik.system.NativeStart.main(Native Method)
这是 getallTransaction() 函数。这是我项目中哈希映射的唯一痕迹
public static void getAllTransaction(){
Cursor localCursor = localDatabase.query(true, TN_TABLE, null, null, null, null, null, null, null) ;
if (localCursor != null) {
localCursor.moveToFirst();
do{
HashMap<String,String> temp = new HashMap<String,String>();
temp.put("Amount", localCursor.getString(localCursor.getColumnIndex("amount")));
temp.put("Category", localCursor.getString(localCursor.getColumnIndex("category")));
temp.put("Date", localCursor.getString(localCursor.getColumnIndex("date")));
temp.put("Description", localCursor.getString(localCursor.getColumnIndex("description")));
temp.put("Id", localCursor.getString(localCursor.getColumnIndex("_id")));
TransactionList.localArrayList.add(temp);
}while (localCursor.moveToNext());
}