删除数据库项后更新Listview

时间:2014-08-18 06:45:41

标签: android sqlite listview

我一直试图在删除项目后让我的listview更新。这是我到目前为止所做的:

myList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        String str = null;

        public void onItemClick(AdapterView<?> arg0, final View view, int arg2, long arg3) {

            //TextView txtview = (TextView)view.findViewById(R.id.txtview);
            final String item = ((TextView) view.findViewById(R.id.txtview)).getText().toString();
            str = item;
            final long arr = arg3;
            final String arg22 = longToString(arg3);

            //Creating an Alert Dialog
            AlertDialog.Builder builder = new AlertDialog.Builder(Home.this);
            builder.setMessage("Are you sure you want to delete the hike " + str + " ?");
            builder.setCancelable(false);
            builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int which) {
                    SQLiteDatabase db1=openOrCreateDatabase("hikeManager", MODE_PRIVATE, null);
                    DatabaseHandler db = new DatabaseHandler(Home.this);
                    String table = "hikes";

                    Cursor c = db1.rawQuery("select id from "+ table + " where  name='"+item+"'", null);
                    int dhike = c.getColumnIndex("name") + 1;

                    try {
                        Hike hike = db.getHike(arr + 1);
                        db.deleteHike(hike);
                        Log.d("DLT", "Deleted hike at index " + arr);
                        //db.updateList(adapter, myList, listItems);
                        adapter.remove(arg22);
                        adapter.notifyDataSetChanged();
                        //updateData();
                        db.close();
                    } catch (CursorIndexOutOfBoundsException e) {
                        Log.d("DLT", "Failed to delete: " + e.getMessage());
                        db.close();
                    }
                    //db.updateList(adapter, myList, listItems);



                }
            });
            builder.setNegativeButton("No", new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
                    dialog.cancel();
                }
            });
            AlertDialog alert = builder.create();
            alert.show();
        }
    });

我有相当多的未使用的代码,因为我尝试了一些不同的方法来实现这一点,但到目前为止都失败了。这是updateData:

private void updateData() {
    // Get all of the notes from the database and create the item list
    DatabaseHandler db = new DatabaseHandler(this);
    final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.txtview, listItems);
    final ListView myList = (ListView) findViewById(R.id.cardListView);

    int num = db.getHikesCount();
    for (int i=1; i<num+1; ++i){
        Hike name = db.getHike(i);
        String nam = name.getName();
        listItems.add(nam);
    }
    myList.setAdapter(adapter);
    db.close();
}

当我在将项添加到非空列表后使用它来更新视图时,updateData确实会产生一些意想不到的后果,但它现在有效。该项目已成功删除,因为我可以关闭该应用程序并重新加载它,该项目将消失。我似乎无法让它为我正确更新。

1 个答案:

答案 0 :(得分:0)

只需使用

adapter.notifyDataSetChanged();