My project that I’ve spent almost 40hrs on is almost complete, but I have one last task to accomplish. My previous problem what that when users clicked to pull up a news RSS feed that app would sometimes hang because it both tried to load the activity and the feed at the same time. This was resolved with asynctask.
Now along with this, I have four buttons that switch between different feeds and essentially just modifying the view. This eliminates reloading the screen or new activity. Well, when I initially fire up this activity, the asynctask works exactly like I want, but it’s not as simple as copying pasting to each of the different views at the bottom. How do I implement asynctask under each of those different buttons/views. You’ll see the onClick at the bottom.
Here is the asynctask section.
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;
}
}
And below here is the onClick section. Obviously with the above i have each readRss, preReadRSS, etc…in the right places in the initial activity, but why won’t Java allow me to place these same asynctask tasks in the onClick section? I just don’t know enought about modifiers yet to know how to place them. Still learning a lot.
public void onClick(View v) {
if (v == hawknation) {
try {
URL rssUrl = new URL("http://www.rss.com/feed");
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();
}}
{
if (myRssFeed!=null)
{
TextView feedTitle = (TextView)findViewById(R.id.feedtitle);
TextView feedDescribtion = (TextView)findViewById(R.id.feeddescribtion);
TextView feedPubdate = (TextView)findViewById(R.id.feedpubDate);
TextView feedLink = (TextView)findViewById(R.id.feedlink);
feedTitle.setText(myRssFeed.getTitle());
feedDescribtion.setText(myRssFeed.getDescription());
feedPubdate.setText(myRssFeed.getPubdate());
feedLink.setText(myRssFeed.getLink());
MyCustomAdapter adapter =
new MyCustomAdapter(this, R.layout.row, myRssFeed.getList());
setListAdapter(adapter);
}
}
I ended up not using my view in question. I just switch between views by opening a different activity on each button.