2013-07-22 53 views
1

我有一个ListActivty自定义基础适配器。我试着点击任何项目后,更新列表,这是我的onListItemClick代码:适配器notifyDataSetChanged只适用于postDelayed

@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不更新列表视图。作为解决方法,我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); 
} 

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

谢谢。

回答

0

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

相关问题