1

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);
}

誰かがなぜこれが起こるのかを説明し、他の解決策があるかどうか教えてもらえますか?

ありがとう。

4

1 に答える 1

0

これは、私が使用しているサンプル コードのバグでした。それを私が直した。詳細については、この要点の最後のコメントを確認してください。修正については、この要旨を確認してください。

于 2013-07-23T13:21:54.933 に答える