如果您对ListViews / RSS feed / Android编程有任何了解,请快速查看此问题。谢谢!
我有一个ListView,其中列出了我的NewsActivity中列出的多个项目(消息),如下所示:
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。我需要帮助的部分是onListItemClick的最后一个函数。该函数创建一个新的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 Activity不显示任何内容。任何帮助将不胜感激!!
答案 0 :(得分:1)
通常最简单的方法是在调用startActivity之前向Intent添加额外的内容。
Intent i = new Intent(this, RSSItem.class);
//pardon the gross use of fields and key names.
//you would pass in the selected item's fields, more than likely using
//its position, not id
i.putExtra("title", title);
i.putExtra("description", description);
i.putExtra("date", date);
startActivityForResult(i, 1);
然后在RSSItem Activity中的onCreate()中,您可以通过
检索该意图@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.rssitem);
Intent receivedIntent = this.getIntent();
//and pick up the extras
title = receivedIntent.getStringExtra("title");
//and so on...
//instantiate views...
TextView titleView = (TextView) findViewById(R.id.title);
titleView.setText(title);
}