如果您對ListViews/RSS feeds/Android編程有一般瞭解,請快速瀏覽一下這個問題。謝謝!如何在多個項目的ListView中顯示新項目中的單個項目(Android)
我有在我的NewsActivity如下多個項(消息)的一個ListView:
public class NewsActivity extends ListActivity {
private List<Message> messages;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.news);
loadFeed();
}
private void loadFeed(){
try{
BaseFeedParser parser = new BaseFeedParser();
messages = parser.parse();
List<String> titles = new ArrayList<String>(messages.size());
List<String> descriptions = new ArrayList<String>(messages.size());
List<String> dates = new ArrayList<String>(messages.size());
for (Message msg : messages){
titles.add(msg.getTitle());
descriptions.add(msg.getDescription());
dates.add(msg.getDate());
}
ArrayAdapter<String> adapter =
new ArrayAdapter<String>(this, R.layout.row,titles);
this.setListAdapter(adapter);
} catch (Throwable t){
Log.e("AndroidNews",t.getMessage(),t);
}
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Intent i = new Intent(this, RSSItem.class);
//id = the id of the item clicked
startActivityForResult(i, 1);
int count = 0;
for (Message msg : messages) {
if(count == id){
//This is obviously not how it should be done.
//But my idea here is that I need to pass the msg to the new RSSItem class.
RSSItem item = new RSSItem(msg);
}
count++;
}
}
}
這NewsActivity顯示我的每個消息的標題的ListView。我需要幫助的部分是ListItemClick的最後一個函數。該函數創建一個新的RSSItem活動。我只需要在該新活動中顯示該項目的標題,日期和描述(這些項目目前都保存在列表中)。以下是我迄今爲止的RSSItem類:
public class RSSItem extends Activity {
private Message rssItem;
private String title;
private String date;
private String description;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.rssitem);
}
public RSSItem(){
//default constructor: must have 0 arguments
}
public RSSItem(Message msg){
rssItem = msg;
title = rssItem.getTitle();
date = rssItem.getDate();
description = rssItem.getDescription();
}
}
所以......目前,得到的RSSItem從NewsActivity消息,並保存標題,日期和說明。如果這應該以更好/不同的方式完成,請讓我知道。
現在我只需要在這個新的RSSItem活動中顯示這些信息(標題,日期,描述)。但是,我不確定這是如何工作/如何去做的。它是否完全由R.layout.rssitem文件確定?這是我的R.layout.rssitem文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
<TextView android:id="@+id/description" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:scrollbars="vertical" />
</LinearLayout>
當前,RSSItem活動不顯示任何內容。任何幫助將不勝感激!!
感謝您爲您的文章檢索意圖!這工作完美。標題,日期和說明現在可用於此新活動。但是,你知道如何顯示這些嗎? (它仍然沒有顯示)。我知道我可以做一些像SetContentView(標題)等,但我不想手動執行此操作。有沒有辦法通過使用layout.rssitem文件來做到這一點?或者我會如何去做這件事? – AndroidRob 2011-04-21 02:33:57
您可能需要查看一些更基本的示例......但這個想法將在調用setContentView(R.layout.rssitem)之後,從您的佈局實例化視圖,以便它們可以顯示您想要的內容。我稍微修改了這篇文章,以提供一個總體思路。 – Maximus 2011-04-21 03:01:11
啊,我現在明白了。我看了一些類似的例子,實際上有過類似的例子。這正是我需要的。非常感謝你的幫助! – AndroidRob 2011-04-21 03:09:47