我有一个扩展片段的类。同样在动作栏中我有标签移动到其他片段。但是我想保存当前片段的数据,这样当我回到这个片段时,数据不会重新加载。我尝试过几个答案,但没有一个有效。请帮忙。
以下是代码:
TopStoriesFragment.java
public class TopStoriesFragment extends Fragment {
public String thehindu = "http://www.thehindu.com/?service=rss";
public String currenturl = thehindu;
AlertDialog levelDialog;
public LayoutInflater inflater;
ListView listView;
int mCurCheckPosition = 0;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_top_stories,
container, false);
feedTitle = (TextView) rootView.findViewById(R.id.feedtitle);
feedDescribtion = (TextView) rootView
.findViewById(R.id.feeddescribtion);
listView = (ListView) rootView.findViewById(android.R.id.list);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
Intent intent = new Intent(getActivity(), ShowDetails.class);
Bundle bundle = new Bundle();
bundle.putString("keyTitle", myRssFeed.getItem(position)
.getTitle());
bundle.putString("keyDescription", myRssFeed.getItem(position)
.getDescription());
bundle.putString("keyLink", myRssFeed.getItem(position)
.getLink());
bundle.putString("keyPubdate", myRssFeed.getItem(position)
.getPubdate());
intent.putExtras(bundle);
startActivity(intent);
}
});
startReadRss();
return rootView;
}
public class RssLoadingTask extends AsyncTask<Void, Void, Void> {
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
displayRss();
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
preReadRss();
}
@Override
protected void onProgressUpdate(Void... values) {
// TODO Auto-generated method stub
// super.onProgressUpdate(values);
}
@Override
protected Void doInBackground(Void... arg0) {
// TODO Auto-generated method stub
readRss();
return null;
}
}
private RSSFeed myRssFeed = null;
TextView feedTitle;
TextView feedDescribtion;
// TextView feedPubdate;
// TextView feedLink;
public class MyCustomAdapter extends ArrayAdapter<RSSItem> {
public MyCustomAdapter(Context context, int textViewResourceId,
List<RSSItem> list) {
super(context, textViewResourceId, list);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
// return super.getView(position, convertView, parent);
View row = convertView;
if (row == null) {
inflater = LayoutInflater.from(getActivity());
row = inflater.inflate(R.layout.row, parent, false);
}
TextView listTitle = (TextView) row.findViewById(R.id.listtitle);
listTitle.setText(myRssFeed.getList().get(position).getTitle());
TextView listPubdate = (TextView) row
.findViewById(R.id.listpubdate);
listPubdate.setText(myRssFeed.getList().get(position).getPubdate());
if (position % 2 == 0) {
listTitle.setBackgroundColor(0xff101010);
listPubdate.setBackgroundColor(0xff101010);
} else {
listTitle.setBackgroundColor(0xff080808);
listPubdate.setBackgroundColor(0xff080808);
}
return row;
}
}
private void startReadRss() {
new RssLoadingTask().execute();
}
private void preReadRss() {
feedTitle.setText("--- wait ---");
feedDescribtion.setText("");
// feedPubdate.setText("");
// feedLink.setText("");
listView.setAdapter(null);
// Toast.makeText(getActivity(), "Reading RSS, Please wait.",
// Toast.LENGTH_LONG)
// .show();
}
private void readRss() {
try {
URL rssUrl = new URL(currenturl);
SAXParserFactory mySAXParserFactory = SAXParserFactory
.newInstance();
SAXParser mySAXParser = mySAXParserFactory.newSAXParser();
XMLReader myXMLReader = mySAXParser.getXMLReader();
RSSHandler myRSSHandler = new RSSHandler();
myXMLReader.setContentHandler(myRSSHandler);
InputSource myInputSource = new InputSource(rssUrl.openStream());
myXMLReader.parse(myInputSource);
myRssFeed = myRSSHandler.getFeed();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void displayRss() {
if (myRssFeed != null) {
Calendar c = Calendar.getInstance();
// String strCurrentTiime = "\n(Time of Reading - "
// + c.get(Calendar.HOUR_OF_DAY)
// + " Hrs. : "
// + c.get(Calendar.MINUTE) + " Mins.)\n" +c.get(Calendar.AM_PM);
SimpleDateFormat sdf = new SimpleDateFormat(
"MMMM-dd-yyyy HH:mm:ss a");
String strCurrentTiime = sdf.format(c.getTime());
feedTitle.setText(myRssFeed.getTitle());
feedDescribtion.setText(strCurrentTiime);
// feedPubdate.setText(myRssFeed.getPubdate());
// feedLink.setText(myRssFeed.getLink());
MyCustomAdapter adapter = new MyCustomAdapter(getActivity(),
R.layout.row, myRssFeed.getList());
listView.setAdapter(adapter);
}
}
}
MainActivity.java
public class MainActivity extends FragmentActivity implements
ActionBar.TabListener {
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
// Tab titles
private String[] tabs = { "Top Stories", "National", "International", "Sports", "Technology", "Business", "Entertainment"};
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initilization
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Adding Tabs
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name)
.setTabListener(this));
}
/**
* on swiping the viewpager make respective tab selected
* */
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
// on changing the page
// make respected tab selected
actionBar.setSelectedNavigationItem(position);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// on tab selected
// show respected fragment view
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
}
我有更多像topstories这样的片段,我想在移动到其他时保存一个数据。