I currently have a project that is a basic RSS Reader. It reads and displays 1 rss feed. However, I want my app to be able to read and display different RSS feeds depending on what button is clicked.
I have a RssService class that basically reads in the url of the RSS feed and sends that input to the Fragment class.
public class RssService extends IntentService {
private static final String RSS_LINK = "http://ift.tt/1GSFojf";
public static final String ITEMS = "items";
public static final String RECEIVER = "receiver";
public RssService() {
super("RssService");
}
@Override
protected void onHandleIntent(Intent intent) {
Log.d(Constants.TAG, "Service started");
List<RssItem> rssItems = null;
try {
PcWorldRssParser parser = new PcWorldRssParser();
rssItems = parser.parse(getInputStream(RSS_LINK));
} catch (XmlPullParserException e) {
Log.w(e.getMessage(), e);
} catch (IOException e) {
Log.w(e.getMessage(), e);
}
Bundle bundle = new Bundle();
bundle.putSerializable(ITEMS, (Serializable) rssItems);
ResultReceiver receiver = intent.getParcelableExtra(RECEIVER);
receiver.send(0, bundle);
}
public InputStream getInputStream(String link) {
try {
URL url = new URL(link);
return url.openConnection().getInputStream();
} catch (IOException e) {
Log.w(Constants.TAG, "Exception while retrieving the input stream", e);
return null;
}
}
}
I don't want to rewrite all the classes implemented to display another RSS feed. I was wondering, If I could do something along the lines of this...
private static final String RSS_LINK1 = "http://ift.tt/1GSFojf";
private static final String RSS_LINK2 = "http://ift.tt/1lI86u6";
private static final String RSS_LINK3 = "http://ift.tt/1InqcJ4";
then...
protected void onHandleIntent(Intent intent) {
Log.d(Constants.TAG, "Service started");
List<RssItem> rssItems = null;
try {
PcWorldRssParser parser = new PcWorldRssParser();
//if (topNewsButton is clicked){
rssItems = parser.parse(getInputStream(RSS_LINK1));
}
//else if(mlbButton is clicked){
rssItems = parser.parse(getInputStream(RSS_LINK2));
}
//else if(nbaButton is clicked){
rssItems = parser.parse(getInputStream(RSS_LINK3));
}
} catch (XmlPullParserException e) {
Log.w(e.getMessage(), e);
} catch (IOException e) {
Log.w(e.getMessage(), e);
}
Bundle bundle = new Bundle();
bundle.putSerializable(ITEMS, (Serializable) rssItems);
ResultReceiver receiver = intent.getParcelableExtra(RECEIVER);
receiver.send(0, bundle);
}
Hope you see what I am trying to do. I thought this is reasonable because, the RssService class is really the one that is calling the actual Feed. Every other class such as RssAdapter, RssFragment etc. seem like all supporting classes. So it seems logical to implement. Hope this is clear. I need help finding the if statements to make this code work.
Here is my class Activity where the user will be clicking on the buttons. I only have 1 button implemented so far which is topNewsButton as you can see, but I want to get the MLB NBA buttons implemented with the specific feeds.
public class RssHubActivity extends Activity implements View.OnClickListener {
Button buttonTopNews;
//Button buttonMlbNews;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rss_hub);
buttonTopNews = (Button)findViewById(R.id.buttonTopNews);
buttonTopNews.setOnClickListener(this);
// buttonMlbNews = (Button)findViewById(R.id.buttonMlbNews);
//buttonMlbNews.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_rss_hub, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.buttonTopNews:
buttonTopNewsClick();
break;
}
/*
switch (v.getId()) {
case R.id.buttonMlbNews:
buttonMlbNewsClick();
break;
}*/
}
private void buttonTopNewsClick()
{
startActivity(new Intent("com.parse.inclassmode.RssActivity"));
}
/*
private void buttonMlbNewsClick()
{
Intent takeUserToMLB = new Intent(RssHubActivity.this, RssMLBActivity.class);
startActivity(takeUserToMLB);
}*/
}
Thanks
Aucun commentaire:
Enregistrer un commentaire