[英]How do I display a single item in a new activity from a ListView of multiple items (Android)
If you have any knowledge of ListViews/RSS feeds/Android programming in general, please take a quick look at this question. 如果您大致了解ListViews / RSS提要/ Android编程,请快速查看此问题。 Thanks! 谢谢!
I have a ListView of multiple items(messages) that are listed in my NewsActivity as follows: 我在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++;
}
}
}
This NewsActivity displays my ListView of the titles of each message. 此NewsActivity显示我的每封邮件标题的ListView。 The part I need help with is the last function onListItemClick. 我需要帮助的部分是onListItemClick的最后一个功能。 The function creates a new RSSItem Activity. 该函数创建一个新的RSSItem活动。 I just need to display the title, date, and description of that item(which are all currently saved in Lists) in this new activity. 我只需要在此新活动中显示该项目的标题,日期和描述(当前全部保存在列表中)。 Here is what I have so far for the RSSItem class: 到目前为止,这是我对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();
}
}
So... currently, RSSItem gets the message from the NewsActivity, and saves the title, date, and description. 所以...当前,RSSItem从NewsActivity获取消息,并保存标题,日期和描述。 If this should be done in a better/different way, please let me know. 如果应该以更好/不同的方式进行此操作,请告诉我。
Now I just need to display this information(title, date, description) in this new RSSItem Activity. 现在,我只需要在此新的RSSItem活动中显示此信息(标题,日期,描述)。 However, I am not exactly sure how this works/how to do it. 但是,我不确定这是如何工作的/如何做。 Is it completely determined by the R.layout.rssitem file? 是否完全由R.layout.rssitem文件确定? Here is my R.layout.rssitem file: 这是我的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>
Currently, the RSSItem Activity doesn't display anything. 当前,RSSItem活动不显示任何内容。 Any help would be greatly appreciated!! 任何帮助将不胜感激!!
Usually the easiest thing to do is to add extra's to your Intent before calling startActivity. 通常,最简单的方法是在调用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);
Then in your onCreate() in the RSSItem Activity, you can retrieve that intent by 然后,在RSSItem活动的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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.