【发布时间】:2013-08-28 14:51:02
【问题描述】:
我所拥有的是一个AsyncTask,,它在ListView 到达屏幕底部项目时立即启动,并在AsyncTask 中将新项目添加到ListView.
代码如下:
@Override
protected void onPostExecute(final List<ItemDailyRecord> records) {
super.onPostExecute(records);
((ActivityHome)context).runOnUiThread(new Runnable() {
public void run() {
for (ItemDailyRecord p : records) {
adapter.add(p);
}
adapter.notifyDataSetChanged();
}
});
}
@Override
protected List<ItemDailyRecord> doInBackground(Void... voids) {
DbAdapterDailyRecord db = new DbAdapterDailyRecord(context);
List<ItemDailyRecord> list = db.getRecordsFromTo(offset, count);
return list;
}
这是一个来自 ListView Adapter 的方法:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(position == getCount() - 1 && hasMoreItems){
HistoryLoaderTask t = new HistoryLoaderTask(position + 1, pageSize, getContext(),this);
t.execute();
footer.setText("Loading . . .");
}
错误信息是(如果我滚动列表视图太快:)
原因:android.view.ViewRootImpl$CalledFromWrongThreadException: 只有创建视图层次结构的原始线程才能触及其 意见。
任何想法如何解决这个问题?
【问题讨论】:
-
onPostExecute()已经在 UI 线程中运行。runOnUiThread()调用没用。 -
在doInBackground中显示你正在使用的代码
-
但如果我删除它,也会出现错误消息:(
-
另外,您不应该将该项目添加到引用该适配器而不是适配器本身的列表中吗?
-
我用“doInBackground”方法更新了我的帖子。
标签: android multithreading listview adapter