【问题标题】:Delete Query And Refresh in ListView in Android (sqlite)Android中ListView中删除查询和刷新(sqlite)
【发布时间】:2012-10-13 05:14:25
【问题描述】:

我是 Java、Android 和 SQLite 的新手,我被困在这上面。我有列,即_id(自动增量)、typeamount(整数)、categorydescription。三个查询。

首先,我无法删除从数据库中检索到的事务(行)。 (请参考随附的 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);

}

Logcat

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());

    }

【问题讨论】:

    标签: android database sqlite android-sqlite


    【解决方案1】:

    首先,我无法删除从数据库中检索到的事务(行)

    第 61 行将arg0.getItemAtPosition(arg2) 的结果转换为Cursor,但返回类型可能是HashMap(如Logcat 输出中所述)。通常你会从数据库查询中得到一个Cursor

    如果您在构建数据库时将每个数据库行的 ID 放入 HashMap,那么您就可以在您的 onClick 事件中将该 ID 传递给您的 deleteTransaction() 调用。所以,你需要

    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(和其他人)以具有更清晰的名称,以便代码更易于理解。

    其次,我需要知道删除条目后如何刷新列表视图

    对数据集进行更改后,您可以调用适配器上的notifyDataSetChanged() 来刷新ListView

    编辑:请注意,SimpleAdapter 用于静态数据,因此您的里程可能会有所不同。最好的解决方案可能是切换到不同类型的适配器,例如ArrayAdapter,或者每次更改数据集时都创建一个新的SimpleAdapter

    【讨论】:

    • 我已经使用哈希映射来检索数据,但这有什么关系。如果类型转换是问题,你建议我应该如何继续......
    • 根据上面的代码很难给出明确的答案,但你可能想做这样的事情:Cursor c = localDbCrud.query(table_name, null, "_id=" + arg0.getItemAtPosition(arg2), null, null, null, null);
    • 我无法在当前类中调用查询。我需要为此在 DbCrud 中调用一个函数。我仍然坚持这个..
    • 听起来您需要一种方法来在从 getItemAtPosition() 获得的整数 ID 和与您的数据库行匹配的相应 _id 值之间进行转换。如果您可以直接从HashMap 中提取_id 值,则不需要Cursor。只需将值从HashMap 传递到deleteTransaction()。没有看到更多代码,我无法提出更准确的建议。
    • 你想要哪一部分代码。我可以上传它,因为我真的卡住了,无论如何也找不到
    猜你喜欢
    • 1970-01-01
    • 2011-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-06
    • 2018-09-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多