Android - Listview delete item and Refresh

后端 未结 9 1706
耶瑟儿~
耶瑟儿~ 2020-12-06 02:16

I am trying to implement ListView with Delete functionality to delete item from the listview. I am successful to delete but failed to refresh the listview after deletetion o

相关标签:
9条回答
  • 2020-12-06 03:08

    if you have the cursor, call requery() before calling notifyDataChanged()

    0 讨论(0)
  • 2020-12-06 03:14

    I think instead of calling the activity again, you should set the adapter to the listview on the alertBox delete option after getting the updated data from the database and putting into listitemDisplay list like this.

    alertDialog.setButton("Delete", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                    try
                    {
                        db.open();
                                 String[] whereArgs={String.valueOf(pkID)};
                        return db.delete(DATABASE_TABLE_4,"pk_pkID == ?",whereArgs); 
                        listitemDisplay = db.getItemFromDB();
                        adapter = new ListView_CustomAdapter(this, listitemDisplay);
                        lview.setAdapter(adapter);
                        db.close();
                    }
                    catch(Exception e)
                    {
    
                    }
            } }); 
    

    This will refresh the listView

    0 讨论(0)
  • 2020-12-06 03:15

    Make a new function outside your onCreate block {something like... getdata()} and inside that insert and get all your data and set to the adapter.
    Then call the function again in your onResume() block. So whenever you will delete the data from the list it will reflect immediately.

    0 讨论(0)
提交回复
热议问题