It was a bug on a sample code I'm using. I fixed it. Check the last comments on this gist for more info, and this gist for the fix
Adapter notifyDataSetChanged only working when postDelayed
-
03-06-2022 - |
Question
I have a custom base adapter for a ListActivty. I'm trying to update the list after any item is clicked, this is my onListItemClick code:
@Override
protected void onListItemClick(ListView listView, View view, int position, long id) {
String pkg = mAdapter.getItem(position).toString();
boolean isProtected = ApplicationUtils.isPackageProtected(this, pkg) != null;
PreferenceUtils.setPreference(mContext, null, pkg, !isProtected, false);
mAdapter.notifyDataSetChanged();
}
However, notifyDataSetChanged doesn't updates the list views. As a workaround I fou
@Override
protected void onListItemClick(ListView listView, View view, int position, long id) {
String pkg = mAdapter.getItem(position).toString();
boolean isProtected = ApplicationUtils.isPackageProtected(this, pkg) != null;
PreferenceUtils.setPreference(mContext, null, pkg, !isProtected, false);
Handler adapterHandler = new Handler();
adapterHandler.postDelayed(new Runnable() {
@Override
public void run() {
mAdapter.notifyDataSetChanged();
}
}, 500);
}
Can someone explain me why does this happens and tell me if is there any other solution?
Thanks.
Solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow