関数内ArrayList
の ListView のデータを含む を返す関数を設定し、 onPostExecute() 内に 設定しますdoInBackground
AsyncTask
notifyDataSetCahnged()
しかし、配列リストにデータがあるにもかかわらず、データはリストビューに反映されませんか? なぜ ?
コード:
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;
}