适配器notifyDataSetChanged仅在postDelayed时工作

时间:2013-07-22 20:56:07

标签: android listview notifydatasetchanged

我有一个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不会更新列表视图。作为一种解决方法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);
}

有人可以解释一下为什么会发生这种情况并告诉我是否有其他解决方案?

感谢。

1 个答案:

答案 0 :(得分:0)

这是我正在使用的示例代码的错误。我修好了它。查看有关this要点的最新评论以获取更多信息,并this要求修复