【发布时间】:2013-10-30 11:31:01
【问题描述】:
我在从 AsynTask 更新 TextView 时遇到了一个奇怪的问题。 在我的适配器的 getView 函数中,我启动了一个 AsyncTask 以计算一个数字并将其显示在屏幕上。
问题是getView函数为单个项目调用了多次,因此计算了我要显示的数次,效率不高。
我经过调查发现,每当我尝试限制对 AsyncTask 的 os 调用次数时,屏幕上都不会显示该数字(没有错误消息或异常)
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.category_item, null);
TextView tvCategory = (TextView) rowView.findViewById(R.id.tvCategory);
TextView tvUnreadNews = (TextView) rowView.findViewById(R.id.tvUnreadNews);
Category cat = mCategoriesList.get(position);
tvCategory.setText(cat.getName());
if(!cat.isInitialized()) //<-- If I delete this line it works, but inefficienly, as the AsyncTask is launched many times repeatedly
{
cat.setIsInitialized(true)
new GetNewsForCategoryTask(cat, tvUnreadNews, mContext).execute(cat.getId());
}
return rowView;
}
这是异步任务。重复调用时正确更新TextView,但类别仅调用一次时不更新值:
public class GetNewsForCategoryTask extends AsyncTask<String, Integer, JSONArray>{
private Context mContext;
private String mCategoryId;
private TextView mTvUnread;
private Category mCategory;
public GetNewsForCategoryTask(Category cat, TextView tvUnread, Context context) {
mTvUnread = tvUnread;
mCategory = cat;
mContext = context;
}
@Override
protected JSONArray doInBackground(String... params) {
mCategoryId = params[0];
...
}
@Override
protected void onProgressUpdate(Integer... values) {
mTvUnread.setText(Integer.toString(values[0]));
}
@Override
protected void onPostExecute(JSONArray result) {
if(result != null && mThrown == null)
{
publishProgress(mCategory.getUnreadNewsSet().size());
}
}
}
有没有人可能是这种奇怪行为的原因?我检查了 AsyncTask 仅在启动它们一次 pro 类别时被正确调用,但只是不更新布局。为什么启动多次专业类别有效?
更新: 我一直在测试,看起来问题出在 getView() 函数上。如果我检查变量是否已初始化,那么只有 ListView 的第一个元素会发生变化……ListView 中其他项目的所有值都会发生变化!
所以看起来 TextView 试图设置的值始终是第一个(ListView 的第一个元素)。
有什么想法???
【问题讨论】:
-
是
mTvUnread异步任务的私有字段吗? -
是的,它是 AsyncTask 的私有字段。我在构造函数上初始化它。
-
我在你的代码中看不到?你能发布完整的适配器和异步任务,以便我可以运行它..
-
mTvUnread 在你声明的地方。
-
我编辑了代码,你可以看到现在声明的地方。
标签: android multithreading android-asynctask textview adapter