我在函数 in 中设置了返回ArrayList
包含 ListView 数据的doInBackground
函数AsyncTask
,并notifyDataSetCahnged()
在 onPostExecute() 中 设置了
但是尽管arraylist中有数据,但daat不会反映在listview上?为什么 ?
代码:
public class E0XMLParsing_RSSExampleActivity extends Activity {
ListView lvFeeds;
FeedsAdapter feedsAdapter;
ArrayList<Feed> listOfFeeds;
ProgressDialog progress;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
listOfFeeds = new ArrayList<Feed>();
lvFeeds = (ListView) findViewById(R.id.listFeeds);
feedsAdapter = new FeedsAdapter(getApplicationContext(), R.layout.row, listOfFeeds);
String url = "http://www.aljazeera.com/Services/Rss/?PostingId=200772215035764169";
progress = ProgressDialog.show(this, "Aljazeera RSS",
"Please wait while downloading the RSS feeds ...");
new DownloadRSS().execute(url);
}
private class DownloadRSS extends AsyncTask<String,Integer,ArrayList<Feed>>{
@Override
protected ArrayList<Feed> doInBackground(String... urls) {
XMLParser parser = new XMLParser();
return parser.getFeedsList(urls[0]);
}
protected void onPostExecute(ArrayList<Feed> listOfFeeds2){
progress.dismiss();
listOfFeeds = listOfFeeds2;
feedsAdapter.notifyDataSetChanged();
}
}
饲料适配器
public class FeedsAdapter extends ArrayAdapter<Feed>{
public ArrayList<Feed> listOfFeeds= null;
Context context;
public FeedsAdapter(Context context,int textViewResourceId,ArrayList<Feed> feeds) {
super(context,textViewResourceId,feeds);
this.listOfFeeds = feeds;
this.context = context;
}
@Override
public View getView(int position, View view,ViewGroup parent){
View v= view;
if (v!=null){
LayoutInflater inflater = (LayoutInflater)context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.row,null);
Feed f = listOfFeeds.get(position);
TextView lblTitle = (TextView) v.findViewById(R.id.lblTitle);
TextView lblDescription = (TextView) v.findViewById(R.id.lblDescription);
TextView lblDate = (TextView) v.findViewById(R.id.lblDate);
lblTitle.setText(f.title);
lblDescription.setText(f.description);
lblDate.setText(f.date);
}
return v;
}