ListActivty 用のカスタム ベース アダプターがあります。アイテムがクリックされた後にリストを更新しようとしています。これは私の onListItemClick コードです:
@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();
}
ただし、notifyDataSetChanged はリスト ビューを更新しません。回避策として
@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);
}
誰かがなぜこれが起こるのかを説明し、他の解決策があるかどうか教えてもらえますか?
ありがとう。