通过后台线程(异步任务)更新 listactivty 内容的最佳实践是什么?
1)一旦我操作适配器的内容,我就调用notifyDataSetChanged()来更新适配器,但当用户滚动或单击列表时,我的应用程序仍然强制关闭。任何防止这种情况的指示都会非常有帮助。
Logcat: java.lang.IllegalStateException: 适配器的内容发生了变化但是ListView没有收到通知。
2) 我应该在哪里更新 listactivity 的内容?在 doInBackground() 或 onProgressUpdate() 内?
3)当用户单击列表项时,我经常遇到崩溃。那么在后台操作期间禁用listactivty上的点击事件会解决问题吗?如果是这样,我不确定如何将项目单击侦听器动态删除或设置为 listactivity。也请指教。
4) 我不认为在后台异步任务执行期间阻止所有 ui 交互是解决问题的唯一方法。我知道有一种简单的方法可以做到这一点,但需要一些帮助。
提前致谢。
这是我的 onCreate...
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fa);
tvStatus=(TextView) findViewById(R.id.tvStatus);
adapter = new SimpleAdapter(
this,
mostPopularList,
R.layout.list_item,
new String[] {"title","author","views","date"},
new int[] {R.id.textView1,R.id.textView2,R.id.textView4,R.id.textView3});
//populateList();
setListAdapter(adapter);
}
我的异步任务...
private class LongOperation extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
// code for adding new listactivty items
}
@Override
protected void onPostExecute(String networkStatus) {
adapter.notifyDataSetChanged();
}
@Override
protected void onPreExecute() {
}
@Override
protected void onProgressUpdate(Void... values) {
}
}
日志猫:
我也很少出现非法状态例外......无法重现它。如果我再次遇到它,将发布 logcat。
FATAL EXCEPTION: main
java.lang.IndexOutOfBoundsException: Invalid index 7, size is 4
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:257)
at java.util.ArrayList.get(ArrayList.java:311)
at android.widget.SimpleAdapter.bindView(SimpleAdapter.java:147)
at android.widget.SimpleAdapter.createViewFromResource(SimpleAdapter.java:126)
at android.widget.SimpleAdapter.getView(SimpleAdapter.java:114)
at android.widget.AbsListView.obtainView(AbsListView.java:1294)
at android.widget.ListView.makeAndAddView(ListView.java:1727)
at android.widget.ListView.fillDown(ListView.java:652)
at android.widget.ListView.fillGap(ListView.java:623)
at android.widget.AbsListView.trackMotionScroll(AbsListView.java:2944)
at android.widget.AbsListView.onTouchEvent(AbsListView.java:2065)
at android.widget.ListView.onTouchEvent(ListView.java:3315)
at android.view.View.dispatchTouchEvent(View.java:3766)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:897)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1671)
at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
at android.app.Activity.dispatchTouchEvent(Activity.java:2086)
at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1655)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1671)
at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
at android.app.Activity.dispatchTouchEvent(Activity.java:2086)
at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1655)
at android.view.ViewRoot.handleMessage(ViewRoot.java:1785)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)