2013-07-22 52 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要點修復

相關問題