[英]getView() in ArrayAdapter not getting called from AsyncTask
In one of my android activity, I've a ListView lvFeedsList
. 在我的Android活动之一中,我有一个
ListView lvFeedsList
。 Each row element in the listView
will contain 3 textViews - RSSFeedName, publishDate & feedLength listView
中的每个行元素将包含3个textViews - RSSFeedName,publishDate和feedLength
The contents of the feeds is retrived from a HTTPRsponse
. 提要的内容是从
HTTPRsponse
。 I'm fetching this response in an AsyncTask
. 我在
AsyncTask
获取此响应。
So, in the doInBackground()
, I've send the HTTPRequest
& received & parsed the response & prepared the ArrayList
containing 3 above mentioned information. 因此,在
doInBackground()
,我已发送HTTPRequest
并接收并解析了响应,并准备了包含3个上述信息的ArrayList
。
Then inside the doInBackground()
only, I'm creating the customized ArrayAdapter
for forming the 3 TextViews
in row element. 然后,仅在
doInBackground()
内部,我将创建自定义的ArrayAdapter
以便在row元素中形成3个TextViews
。 My intetions are to set this adapter on ListView in onPostExecute()
. 我的想法是
ListView in onPostExecute()
上设置此适配器。
But, when I run the application, the ListView does not display anything. 但是,当我运行该应用程序时,ListView不会显示任何内容。
I tried to debug & it seems like getView()
in the ArrayAdapter
class is not getting called. 我尝试调试&似乎未调用
ArrayAdapter
类中的getView()
。 (But I'm not sure if this is the reason). (但是我不确定这是否是原因)。
Here is the code, sorry for the length...it seemed necessary. 这是代码,很抱歉,它的长度...这似乎很有必要。
Activity Code: 活动代码:
public class GenericFeedsActivity extends Activity{
private ListView lvFeedsList;
private ArrayList<FeedsClass> feedList;
protected void onCreate(Bundle savedInstanceState) {
...
lvFeedsList = (ListView) findViewById(R.id.lvFeedsList);
lvFeedsList.setOnItemClickListener(this);
lvFeedsList.setEnabled(false);
...
new AsyncResponseHandler(this).execute();
}
class AsyncResponseHandler extends AsyncTask {
Context context;
FeedListAdapter adapter;
public AsyncResponseHandler(Context c) {
this.context = c;
}
@Override
protected Object doInBackground(Object... params) {
...
/*
* Sending HTTPRequest to a URL & getting list of feeds
* Saving this list of feeds in a ArrayList -feedList, containing elements of type FeedsClass (declared above)
* Below line parses the HTTPResponse XML & stores various information in feedList.
*/
feedList = utils.parseRssResponseXML(in); // Working fine, geeting elements
adapter = new FeedListAdapter(
GenericFeedsActivity.this, feedList);
in.close();
return null;
}
protected void onPostExecute(Object e) {
// Setting Arrayadapter
lvFeedsList.setAdapter(adapter);
lvFeedsList.setEnabled(true);
}
}
}
Adapter Code: 适配器代码:
public class FeedListAdapter extends ArrayAdapter {
private Context context;
private ArrayList<FeedsClass> feedList;
public FeedListAdapter(Context c, ArrayList<FeedsClass> data) {
super(c, R.layout.rowlayout);
this.context = c;
this.feedList = data;
}
class ViewHolder {
TextView tvFeedName;
TextView tvFeedPubDate;
TextView tvFeedLength;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ViewHolder holder = null;
if (row == null) {
LayoutInflater inflator = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflator.inflate(R.layout.rowlayout, null);
holder = new ViewHolder();
holder.tvFeedName = (TextView) row.findViewById(R.id.tvFeedName);
holder.tvFeedPubDate = (TextView) row.findViewById(R.id.tvFeedPubDate);
holder.tvFeedLength = (TextView) row.findViewById(R.id.tvFeedLength);
row.setTag(holder);
} else {
holder = (ViewHolder) row.getTag();
}
// Getting values of feedName, publishDate & feedLength
String feedName = feedList.get(position).getTitle();
String feedDate = feedList.get(position).getPublishDate();
String feedLength = feedList.get(position).getStreamLength();
holder.tvFeedName.setText(feedName);
holder.tvFeedPubDate.setText(feedDate);
holder.tvFeedLength.setText(feedLength);
}
return row;
}
}
The issue is that you are subclassing ArrayAdapter
. 问题是您在
ArrayAdapter
。 This doesn't work because ArrayAdapter
internally thinks you do not have any elements in your data; 这是行不通的,因为
ArrayAdapter
内部认为您的数据中没有任何元素。 it doesn't just magically know to look in the lvFeedsList
variable because the data set it uses is internal. 它不仅神奇地知道要查看
lvFeedsList
变量,因为它使用的数据集是内部的。
Instead, in your constructor make sure to call this constructor instead: 相反,请确保在构造函数中调用此构造函数:
Adapter code: 适配器代码:
public FeedListAdapter(Context c, ArrayList<FeedsClass> data) {
super(c, R.layout.rowlayout, data); // add 'data'
this.context = c;
this.feedList = data;
}
Which will make everything work correctly. 这将使一切正常工作。
adapter.notifyDataSetChanged() adapter.notifyDataSetChanged()
could help at the end on of AsyncResponseHandler.onPostExecute(). 可以在AsyncResponseHandler.onPostExecute()的末尾提供帮助。 If not - check whether ArrayList which hold data for adapter is empty or not.
如果不是,请检查保存适配器数据的ArrayList是否为空。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.