[英]Updating TextView from AsyncTask launched from the adapter (getView)
I'm having a strange problem updating a TextView from an AsynTask. 我在从AsynTask更新TextView时遇到一个奇怪的问题。 In the getView function of my adapter I launch an AsyncTask in order to calculate a number and show it on the screen. 在适配器的getView函数中,我启动AsyncTask以便计算一个数字并将其显示在屏幕上。
The problem is that the getView function is called several times for a single item, thus is calculating several times the numbers I want to show, what is not very efficient. 问题在于,单个项目多次调用getView函数,因此计算出我想显示的数字的次数,这并不是很有效。
I've been investigated and realized that whenever I try to limit the number os calls to the AsyncTask, the number is not shown on screen (with no error messages or exceptions) 经过调查,我意识到,每当我尝试将os调用的次数限制为AsyncTask时,该数目都不会显示在屏幕上(没有错误消息或异常)
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;
}
This is the AsyncTask. 这是AsyncTask。 Updates the TextView properly when is calling repeatedly, but does not update the value when called only once for category: 重复调用时正确更新TextView,但仅对category调用一次时不更新值:
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());
}
}
} }
Does anybody what could be the reason for this weird behaviour? 有人会是这种奇怪行为的原因吗? I checked that the AsyncTask are properly called when launching them only once pro Category, but just don't update the layout. 我检查了一次仅在专业类别中启动AsyncTask时是否正确调用了它们,但只是不更新布局。 Why launching the several times pro Category works? 为什么要启动多次亲类别的作品?
UPDATE: I've been testing and looks like the problem is with the getView() function. 更新:我一直在测试,看起来问题出在getView()函数。 If I check if the variable has been initialized then only the first element of the ListView changes...with all the values of the other items in the ListView!! 如果我检查变量是否已初始化,则仅ListView的第一个元素会发生变化……而ListView中其他项目的所有值都将变!
So looks like the TextView is trying to set the value is always the first one (first element of the ListView). 因此,看起来TextView试图设置的值始终是第一个(ListView的第一个元素)。
Any idea??? 任何想法???
Look at this: 看这个:
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());
}
I think that you have to call setIsInitialized(true)
on mCategoriesList.get(position)
, not on cat
, because when getView()
is called again, mCategoriesList.get(position)
always returns uninitialized one, because you call setIsInitialized(true)
on cat
, not in mCategoriesList
. 我认为您必须在mCategoriesList.get(position)
setIsInitialized(true)
上调用setIsInitialized(true)
,而不是在cat
上调用setIsInitialized(true)
,因为当再次调用getView()
时, mCategoriesList.get(position)
始终返回未初始化的值,因为您调用了setIsInitialized(true)
在cat
,不在mCategoriesList
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.