我正在尝试使用 ListFragment 和 ViewPager 制作一个 RSS 阅读器,这是我第一次使用 ListFragment 和 ViewPager。我有六个标签,每个标签都有一个 ListFragment。我已经为每个列表加载了一个类别的提要。我已经使用 AsyncTask 来做到这一点。第一次一切正常。但是当我来回滑动标签时,一切都搞砸了。我得到每个页面上的列表,但列表项大小为 0。所以我得到 IndexOutOfBoundsException。我一直在寻找解决方案近 4 天。但没有找到运气。我认为 onResume() 函数中存在一些错误。但我不确定那是什么。我已经为一个 ListFragment 放置了代码。我哪里出错了?
package com.example.feed;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.support.v4.app.ListFragment;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;;
@SuppressLint("NewApi")
public class LatestNews extends ListFragment {
private ArrayList<RSSItem> itemlist = null;
private RSSLatestAdaptor rssadaptor = null;
Bundle savedBundle = null;
public static LatestNews newInstance(String text) {
LatestNews pageFragment = new LatestNews();
Bundle bundle = new Bundle();
bundle.putString("test", text);
pageFragment.setArguments(bundle);
return pageFragment;
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
// return inflater.inflate(R.layout.sun_fragment, container, false);
savedBundle = savedInstanceState;
LinearLayout fragview = (LinearLayout) inflater.inflate(R.layout.main,
container, false);
itemlist = new ArrayList<RSSItem>();
Log.e("Alert", "Latest News called");
initList();
// Log.d("List", ""+RefreshState.latestAdapter);
ActionBar actionBar = getActivity().getActionBar();
actionBar.show();
// actionBar.setTitle("Latest");
return fragview;
}
private void initList() {
// TODO Auto-generated method stub
int refreshState = RefreshState.latestNews;
if(refreshState == 0)
new RetrieveRSSFeeds().execute();
else {
rssadaptor = new RSSLatestAdaptor(getActivity(),
R.layout.rssitemview, RefreshState.latestAdapter);
Log.d("Saved List", ""+rssadaptor.toString());
Log.d("List Count", ""+rssadaptor.getCount());
setListAdapter(rssadaptor);
rssadaptor.notifyDataSetChanged();
}
}
public void onResume() {
super.onResume();
rssadaptor.notifyDataSetChanged();
// super.onResume();
}
private void retrieveRSSFeed(String urlToRssFeed, ArrayList<RSSItem> list) {
try {
URL url = new URL(urlToRssFeed);
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
XMLReader xmlreader = parser.getXMLReader();
RSSParser theRssHandler = new RSSParser(list);
xmlreader.setContentHandler(theRssHandler);
InputSource is = new InputSource(url.openStream());
xmlreader.parse(is);
} catch (Exception e) {
e.printStackTrace();
}
}
private class RetrieveRSSFeeds extends AsyncTask<Void, Void, Void> {
private ProgressDialog progress = null;
@Override
protected Void doInBackground(Void... params) {
retrieveRSSFeed("http://www.example.com/feed", itemlist);
RefreshState.latestAdapter = itemlist;
rssadaptor = new RSSLatestAdaptor(getActivity(),
R.layout.rssitemview, itemlist);
return null;
}
@Override
protected void onCancelled() {
super.onCancelled();
}
@Override
protected void onPreExecute() {
progress = ProgressDialog.show(getActivity(), null,
"Loading List...");
super.onPreExecute();
}
@Override
protected void onPostExecute(Void result) {
setListAdapter(rssadaptor);
progress.dismiss();
RefreshState.latestNews = 1;
super.onPostExecute(result);
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
}
public class RSSLatestAdaptor extends ArrayAdapter<RSSItem> {
private List<RSSItem> objects = null;
public RSSLatestAdaptor(Context latestNews, int textviewid,
ArrayList<RSSItem> objects) {
super(latestNews, textviewid, objects);
this.objects = objects;
}
@Override
public int getCount() {
return ((null != objects) ? objects.size() : 0);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public RSSItem getItem(int position) {
return ((null != objects) ? objects.get(position) : null);
}
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (null == view) {
LayoutInflater vi = (LayoutInflater) getActivity()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = vi.inflate(R.layout.rssitemview, null);
}
RSSItem data = objects.get(position);
if (null != data) {
TextView title = (TextView) view.findViewById(R.id.txtTitle);
TextView date = (TextView) view.findViewById(R.id.txtDate);
TextView author = (TextView) view.findViewById(R.id.txtAuthor);
title.setText(data.title);
String dateCut = data.date.substring(0, 16);
date.setText("on " + dateCut);
author.setText("by " + data.creator);
}
return view;
}
}
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
RSSItem data = itemlist.get(position);
RSSItem data1 = itemlist.get(position + 1);
// Intent intent = new
// Intent(Intent.ACTION_VIEW,Uri.parse(data.author));
// startActivity(intent);
Log.d("Description", data.description);
Intent detailRss = new Intent(getActivity(), RSSItemDetail.class);
detailRss.putExtra("title", data.title);
detailRss.putExtra("date", data.date);
detailRss.putExtra("creator", data.creator);
detailRss.putExtra("description", data1.description);
detailRss.putExtra("link", data.link);
// Log.d("Link to post", data.link);
startActivity(detailRss);
}
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getActivity().getMenuInflater().inflate(R.menu.refresh_button, menu);
getActivity().getMenuInflater().inflate(R.menu.rss_home, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_go_web:
String url = "http://www.example.com";
Intent browserIntent = new Intent(Intent.ACTION_VIEW);
browserIntent.setData(Uri.parse(url));
startActivity(browserIntent);
break;
case R.id.action_about:
Intent aboutIntent = new Intent(getActivity(), About.class);
startActivity(aboutIntent);
break;
case R.id.action_refresh:
// Intent refreshIntent = new Intent(RssReaderActivity.this,
// RssReaderActivity.class);
// startActivity(refreshIntent);
new RetrieveRSSFeeds().execute();
break;
default:
break;
}
return true;
}
}