我在自己的应用中将RSS Feed解析为列表视图。我试图让它为rss中的每个项目拉链接,并在单击列表视图中的项目时将其打开。基本上我已经有了一切,但不确定在本声明之后我应该使用什么编码。
public void onItemClick(AdapterView parent,View view,int position,long id){
我知道这是我的代码用于打开保存在rss文件中的URL但不确定如何从此处检索它的位置。非常感谢任何帮助。
公共类MainActivity扩展了Activity实现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) {
}
} `
答案 0 :(得分:1)
我只是坚持要通过this
Tutorial
并下载完整的来源并让它正常运行。这将清除您的查询,告知它是如何工作的,还包括使用您将学习的几个XML
Parser
(SAX,DOM,XmlPullParser,其他一些)
<强>更新强>
您可以使用getSelectedItemPosition()
并从ListView
获取所选值。然后进行显示RSS的进一步过程。
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// get the selected item and do the further process
listview.getItemAtPosition(position);
}