ListView not “refreshing” after item is deleted from it

后端 未结 6 1783
半阙折子戏
半阙折子戏 2021-01-28 16:27

I am trying to figure out how I \"Refresh\" the list view the user is on when an item from my listview is deleted. I have tried notifyDataSetChanged() but to no avail, which is

6条回答
  •  Happy的楠姐
    2021-01-28 17:08

    Your item is being deleted from database but the listview is not updating visually.Setting adapter is a solution but it will reset the list from start which not make a good experience for user to scroll all the time again.You can do one thing when you removing item from database at the same time you can remove selected item from your list. I think your listview listener should look like this in your activity:

    listview.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView parent, View view,
                        int position, long id) {
                    deleteButtonClicked(postion);
                }
            });
    
    
    
    public void deleteButtonClicked(int position){
               int count=dbHandler.deleteExerciseFromDatabase(exerciseClickedbyUser, workoutClicked);
        if(count>0){
            list.remove(position);
    
                edsAdapter.notifyDataSetChanged();
                Toast.makeText(getBaseContext(),"Exercise Deleted", Toast.LENGTH_SHORT).show();
        }
    }
    

提交回复
热议问题