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
Thanks for all the help guys, but in the end the answer I came up with is quite different from yours. I ended up using his method [1]: http://jmsliu.com/2444/click-button-in-listview-and-get-item-position.html/ "here" which worked absolutely perfect for me. Anyways this was what did for those who might run into the same issue.
STICK THIS CODE IN THE GetView() method LOCATED IN .JAVA FILE INFLATES THE XML FILE.
Button deleteButton = (Button) customView.findViewById(R.id.deleteButton);
deleteButton.setTag(position);
Then add this into your button click listener/ onClick method
int position = (Integer) view.getTag();
list.remove(position);
adapter.notifyDataSetChanged();
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();
}
}
You can perfectly remove item from adapter by using following code.
mCollection.remove(position);
mListLayout.removeAllViews();
notifyDataSetChanged();
You seem no change in listview because your ArrayList or Array you assigned to the adapter has no changes. Please remove the item from the ArrayList or array too. And you just have to call notifyDataSetChanged() ., ie, there is no need of calling listview.setAdapter(adapter) again.
EDIT :
please replace your customArrayAdapter with the below shown code
private ArrayList<String> workouts;
public CustomExerciseAdapter(Context context, ArrayList<String> workouts) {
super(context, R.layout.exercise_custom_row, workouts);
this.workouts = workouts;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(getContext());
View customView = inflater.inflate(R.layout.exercise_custom_row, parent, false);
/////here change to
String singleExerciseItem = (String)workouts.get(position);
TextView exerciseTV = (TextView) customView.findViewById(R.id.exerciseTV);
exerciseTV.setText(singleExerciseItem);
return customView;
}
public void setList(ArrayList<String> workouts){
this.workouts = workouts;
notifyDatasetChanged();
}
}
In your delete method, after updating database and your list, call adapter.setList(workouts),. it may do the trick for you.
In my app, I don't use notifyDataSetChanged
at all. To refresh the ListView, I simply run the database query again and use CursorAdapter.changeCursor. It will automatically call notifyDataSetChanged
as needed.
after removing items always add the below two lines
notifyDataSetChanged();
notifyDataSetInvalidated();
notifydatasetchanged will see for any changes in the list and notifydatasetinvalidated will check if there is any item removed then it will update the list