用户点击项目android后更新listview

时间:2014-04-22 23:28:56

标签: java android listview notifydatasetchanged

我有一个arraylist存储来自数据库的数据并将其显示在列表视图中,当用户点击数据库中的数据更新时,我想知道如何刷新列表视图中的项目以显示更新的数据

//Adding item to ArrayList
            Cursor cursor2=shoppingListDB.rawQuery("SELECT * FROM ITEMS;", null);

            if  (cursor2.moveToFirst()){
                addArrrayList.clear();
                do{
                    String itemName = cursor2.getString(cursor2.getColumnIndex("ITEM_NAME"));

                    addArrrayList.add(itemName);
                }
                while (cursor2.moveToNext());


                //====CODE FOR SHOWING DATA AS A SIMPLE LIST ITEM=========================================

                ArrayAdapter <String> adapter=new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,addArrrayList);
                view_list.setAdapter(adapter);
            }
            else{
                Toast.makeText(getApplicationContext(), "DATA NOT AVAILABLE", 3000).show();
            }
            cursor2.close();

这是我的setOnItemClickListener代码

view_list.setOnItemClickListener(new OnItemClickListener()
    {
        @Override
         public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3)
    {
             String click = (addArrrayList.get(arg2).toString());
             String[] split = click.split("\\s+");
             String fSplit = split[0];
             String update = "UPDATE ITEMS SET FOUND = 'YES' WHERE ITEM_NAME =" + "'"+fsplit+"'";
            shoppingListDB.execSQL(update);
            Toast.makeText(getApplicationContext(),  " " + fSplit,3000).show();

    }

    });

如何使用notifyDataSetChanged刷新列表视图?

1 个答案:

答案 0 :(得分:0)

这可能是一种清除Arraylist的简单方法,它是适配器的参数。再次填写并调用notifyDataSetChanged。

addArrrayList.clear();
reFillYourArrayListWithUpdatedData(); /*first code part of your question*/
adapter.notifyDataSetChanged();

修改

我认为问题在于

shoppingListDB.execSQL(update);

行。你必须使用

public int update (String table, ContentValues values, String whereClause, String[] whereArgs)

而不是execSQL()

这是docs;

execSQL update