I have an RSS feed parsed into a list view in my app. I’m trying to make it pull the link for each item in the rss and open it when I click the item in the listview. basicly i have everything already in place but not sure what coding i should use after this statement.
public void onItemClick(AdapterView parent, View view, int position, long id) {
I know that this is where my code should be for opening the url that is saved in the rss file but not sure how to retrieve it from here. Any help is greatly appreciated.
public class MainActivity extends Activity implements OnItemClickListener {
//RSS Feed URL
private final String CGR_FEED_URL = "http://www.mychurchevents.com/Calendar/RSS.ashx?days=7&ci=G1M7G1N8K5G1N8N8H2&igd=";
//XML Widgets
private ListView listview_episodes;
private ProgressBar progress_bar;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//XML Widgets by ID
listview_episodes = (ListView) findViewById(R.id.listview_episodes);
listview_episodes.setOnItemClickListener(this);
progress_bar = (ProgressBar) findViewById(R.id.progress_bar);
//Make Progress Bar Invisible
progress_bar.setVisibility(ProgressBar.INVISIBLE);
new ArrayList<String>();
new ArrayList<String>();
downloadEpisodes(CGR_FEED_URL);
}
private void downloadEpisodes(String Url) {
//Make Progress Bar Visible While Downloading Feed
progress_bar.setVisibility(ProgressBar.VISIBLE);
Log.d("CGRParser", "Downloading Feed");
//Start an ASync Thread to take care of Downloading Feed
new DownloadEpisodes().execute(Url);
}
private class DownloadEpisodes extends AsyncTask<String, Integer, ArrayList<Episode>> {
@Override
protected ArrayList<Episode> doInBackground(String... url) {
//Download and Parse Feed
XmlFeedParser parser = new XmlFeedParser();
ArrayList<Episode> episodes = new ArrayList<Episode>();
episodes = parser.parse(url[0]);
return episodes;
}
@Override
protected void onPostExecute(ArrayList<Episode> result) {
//Feed has been Downloaded and Parsed, Display Data to User
Log.d("CGRParser", "Feed Download Complete");
displayEpisodes(result);
}
}
private void displayEpisodes(ArrayList<Episode> episodes) {
//Create String Arrays to seperate titles and dates
Log.d("CGRParser", "Displaying Episode Titles To User");
ArrayList<String> episode_titles = new ArrayList<String>();
ArrayList<String> episode_dates = new ArrayList<String>();
for (Episode episode : episodes) {
Log.d("CGRParser", "Episode Title: " + episode.getTitle());
episode_titles.add(episode.getTitle());
episode_dates.add(episode.getDate());
}
//Create a ListAdapter to Display the Titles in the ListView
ListAdapter adapter = new ArrayAdapter<String>(this, R.layout.episode_row, R.id.title, episode_titles);
listview_episodes.setAdapter(adapter);
//Set Progress Bar Invisible since we are done with it
progress_bar.setVisibility(ProgressBar.INVISIBLE);
}
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
}
`
I would just insist to go through
thisTutorialand download the complete source and get it working. This will clear up your queries that how it works and also it has included using severalXMLParser(SAX, DOM, XmlPullParser, some others) that you will get to learn.UPDATE:
You can use
getSelectedItemPosition()and get the selected value from theListView. And then do the further process of showing the RSS.