删除查询和刷新的ListView在Android中(源码) [英] Delete Query And Refresh in ListView in Android (sqlite)

查看:226
本文介绍了删除查询和刷新的ListView在Android中(源码)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的Java,Android和SQLite和我被困在此。我有列,即 _id (自动增量),键入金额(INT),类别说明。三个查询。

首先,我无法删除数据库中检索事务处理(行)。 (详情请参阅附logcat的)。

其次,我需要知道如何删除条目之后刷新的ListView。

第三,傻问题,我认为。如果我打开一次数据库和检索数据,它这样做。此外,在不关闭数据库,如果我删除事务不删除它,并给出了错误数据库无法打开。此外,如果我检索后关闭数据库,然后在删除的时间再次打开,它的工作原理。我不明白这一点。主要的问题是第一位的,但请你回答,如果你知道上述任何

 最终的ListView LV = getListView();
    localArrayList.clear(); //要停止愚蠢的重复问题
    localDbCrud =新DbCrud(本);
    localAdapter =新SimpleAdapter(
            这个,
            localArrayList,
            R.layout.transaction_list_item,
            新的String [] {金额,类别,日期,说明},
            新的INT [] {R.id.tli_amount,R.id.tli_category,R.id.tli_date,R.id.tli_desc});    localDbCrud.open();
    DbCrud.getAllTransaction(); //使用HashMap的localHashMap,localHashMap.put(localArrayList)环路
    localDbCrud.close();
    lv.setOnItemLongClickListener(新OnItemLongClickListener(){        公共布尔onItemLongClick(最终适配器视图<>为arg0,ARG1查看,
                 最终诠释ARG2,最终长ARG3){
            // TODO自动生成方法存根
            AlertDialog.Builder ladbuilder =新生成器(TransactionList.this);
            ladbuilder.setTitle(选择您的选择);
            ladbuilder.setItems(R.array.alertdialog_prompt,新DialogInterface.OnClickListener(){                公共无效的onClick(DialogInterface对话,诠释selected_item){
                    // TODO自动生成方法存根
                    如果(selected_item == 0){
                        localDbCrud.open();
                        HashMap的itemMap =(HashMap的)localAdapter.getItem(ARG2);
                        INT ITEM_ID =(整数)itemMap.get(ID);
                        DbCrud.deleteTransaction(ITEM_ID);
                        //最终诠释ITE =(整数)arg0.getItemAtPosition(ARG2);
                         //最终诠释ITEM_ID = c.getInt(c.getColumnIndex(DbCrud.TN_ID));
                        //DbCrud.deleteTransaction(item_id);
                        localDbCrud.close();                    }
                    其他{
                        //更新code                    }
                }
            });
            AlertDialog localad = ladbuilder.create();
            localad.show();
            返回false;
        }
    });    localDbCrud.close();    setListAdapter(localAdapter);}

logcat的

  1月10日至13日:21:26.804:E / AndroidRuntime(22023):致命异常:主要
1月10日至13日:21:26.804:E / AndroidRuntime(22023):java.lang.ClassCastException:java.lang.String中
1月10日至13日:21:26.804:E / AndroidRuntime(22023):在com.hishighness.budgetracker.TransactionList $ 1 $ 1.onClick(TransactionList.java:62)
1月10日至13日:21:26.804:E / AndroidRuntime(22023):在com.android.internal.app.AlertController $ AlertParams $ 3.onItemClick(AlertController.java:878)
1月10日至13日:21:26.804:E / AndroidRuntime(22023):在android.widget.AdapterView.performItemClick(AdapterView.java:284)
1月10日至13日:21:26.804:E / AndroidRuntime(22023):在android.widget.ListView.performItemClick(ListView.java:3701)
1月10日至13日:21:26.804:E / AndroidRuntime(22023):在android.widget.AbsListView $ PerformClick.run(AbsListView.java:1970)
1月10日至13日:21:26.804:E / AndroidRuntime(22023):在android.os.Handler.handleCallback(Handler.java:587)
1月10日至13日:21:26.804:E / AndroidRuntime(22023):在android.os.Handler.dispatchMessage(Handler.java:92)
1月10日至13日:21:26.804:E / AndroidRuntime(22023):在android.os.Looper.loop(Looper.java:130)
    1月10日至13日:21:26.804:E / AndroidRuntime(22023):在android.app.ActivityThread.main(ActivityThread.java:3687)
1月10日至13日:21:26.804:E / AndroidRuntime(22023):在java.lang.reflect.Method.invokeNative(本机方法)
1月10日至13日:21:26.804:E / AndroidRuntime(22023):在java.lang.reflect.Method.invoke(Method.java:507)
1月10日至13日:21:26.804:E / AndroidRuntime(22023):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:842)
1月10日至13日:21:26.804:E / AndroidRuntime(22023):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
1月10日至13日:21:26.804:E / AndroidRuntime(22023):在dalvik.system.NativeStart.main(本机方法)

这里的getallTransaction()功能。本是哈希映射在我的项目中唯一跟踪

 公共静态无效getAllTransaction(){    光标localCursor = localDatabase.query(真,TN_TABLE,NULL,NULL,NULL,NULL,NULL,NULL,NULL);
    如果(localCursor!= NULL){        localCursor.moveToFirst();
            做{
                HashMap的<字符串,字符串>临时=新的HashMap<字符串,字符串>();
                temp.put(数量,localCursor.getString(localCursor.getColumnIndex(额)));
                temp.put(类别,localCursor.getString(localCursor.getColumnIndex(类)));
                temp.put(日期,localCursor.getString(localCursor.getColumnIndex(DATE)));
                temp.put(描述,localCursor.getString(localCursor.getColumnIndex(说明)));
                temp.put(ID,localCursor.getString(localCursor.getColumnIndex(_ ID)));
                TransactionList.localArrayList.add(临时);            }而(localCursor.moveToNext());    }


解决方案

  

首先,我无法删除数据库中检索事务处理(行)


61号线是铸造 arg0.getItemAtPosition(ARG2)的结果光标,但返回类型为大概的HashMap (因为它说,在logcat的输出)。通常,你会得到一个光标从数据库中查询。

如果你把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((字符串)itemMap.get(ID));
DbCrud.deleteTransaction(ITEM_ID);
localDbCrud.close();

我也建议改名 ARG2 (和其他人)有清晰的名称,以便code是更容易遵循。


  

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


您可以致电 notifyDataSetChanged()您的适配器,刷新的ListView 您所做的更改后数据集。

编辑:请注意, SimpleAdapter 是为静态数据,所以你的里程可能会有所不同。最好的解决办法可能是切换到不同类型的适配器,如 ArrayAdapter ,或作出新的 SimpleAdapter 每一次更改的数据集。

I'm new to Java, Android, and SQLite, and I'm stuck at this. I have columns, namely _id (autoincrement), type, amount (int), category, and description. Three queries.

Firstly, I am not able to delete a transaction (row) retrieved from database. (Please refer to the attached Logcat).

Secondly, I need to know how to refresh the ListView after deleting the entry.

And third, the silly issue I think. If I open the database once and retrieve data, it does so. Further, without closing the database, if I delete a transaction it does not delete it and gives the error "database not open". Further, if I close the database after retrieval and then open again at the time of deletion, it works. I dont get it. Main issue is the first one, but please answer if you know any of the above

    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)

here's the getallTransaction() function.This is the only trace of hash map in my project

    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

Line 61 is casting the result of arg0.getItemAtPosition(arg2) to Cursor, but the return type is probably HashMap (as it says in the Logcat output). Normally you get a Cursor from a database query.

If you put the ID for each database row into the HashMap when you're building it, then you'll be able to pass that ID to your deleteTransaction() call in your onClick event. So, you need

temp.put("Id", localCursor.getInt(localCursor.getColumnIndex("_id")));

in getAllTransaction(), and then revise your onClick() method to do something like this:

localDbCrud.open();
HashMap itemMap = (HashMap)localAdapter.getItem(arg2);
int item_id = Integer.parseInt((String)itemMap.get("Id"));
DbCrud.deleteTransaction(item_id);
localDbCrud.close();

I would also suggest renaming arg2 (and others) to have clearer names so that the code is easier to follow.

Secondly, I need to know how to refresh the listview after deleting the entry

You can call notifyDataSetChanged() on your adapter to refresh the ListView after you've made a change to the dataset.

EDIT: Please note that SimpleAdapter is meant for static data, so your mileage may vary. The best solution is probably to switch to a different type of adapter, such as ArrayAdapter, or make a new SimpleAdapter every time you change the dataset.

这篇关于删除查询和刷新的ListView在Android中(源码)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆