我是 Java、Android 和 SQLite 的新手,我被困在了这里。我有列,即 _id
(自动增量)、type
、amount
(int)、category
和 描述
。三个查询。
首先,我无法删除从数据库中检索到的事务(行)。 (请引用随附的 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() 函数。这是我项目中 HashMap 的唯一踪迹
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());
}
最佳答案
Firstly, I am not able to delete a transaction (row) retrieved from database
第 61 行将 arg0.getItemAtPosition(arg2)
的结果转换为 Cursor
,但返回类型可能是 HashMap
(因为它在 Logcat 输出中说)。通常,您会从数据库查询中获得一个 Cursor
。
如果您在构建时将每个数据库行的 ID 放入 HashMap
,那么您就可以将该 ID 传递给您的 deleteTransaction()
调用您的 onClick
事件。所以,你需要
temp.put("Id", localCursor.getInt(localCursor.getColumnIndex("_id")));
在 getAllTransaction()
中,然后修改您的 onClick()
方法来执行如下操作:
localDbCrud.open();
HashMap itemMap = (HashMap)localAdapter.getItem(arg2);
int item_id = Integer.parseInt((String)itemMap.get("Id"));
DbCrud.deleteTransaction(item_id);
localDbCrud.close();
我还建议重命名 arg2
(和其他名称)以具有更清晰的名称,以便代码更易于理解。
Secondly, I need to know how to refresh the listview after deleting the entry
您可以在对数据集进行更改后调用适配器上的 notifyDataSetChanged()
来刷新 ListView
。
编辑:请注意 SimpleAdapter
适用于静态数据,因此您的情况可能会有所不同。最好的解决方案可能是切换到不同类型的适配器,例如 ArrayAdapter
,或者在每次更改数据集时创建一个新的 SimpleAdapter
。
关于android - Android(sqlite)中ListView中的删除查询和刷新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12849622/