简体   繁体   English

如何通过多个项目的ListView在新活动中显示单个项目(Android)

[英]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.

相关问题 如何从ListView Android中选择多个项目 - how do i able select multiple items from listview android 如何强制ListView显示单个项目 - How do I force a ListView to display a single item 在android中单击Listview的单个项目时选择ListView的多个项目 - Multiple Items selected of ListView when Click on Single Item of Listview in android Android:如何通过单击ListView中的项目从一种活动切换到另一种活动 - Android: how do i switch from one activity to another by clicking items from the ListView Android:我在操作栏中有一个操作栏和listview。 单击列表视图中的项目时,如何在新视图中显示图像 - Android: I have an actionbar and listview within the actionbar. How do I display an image in new view when I click on an item in the listview Android在新活动中打开listview项,然后在活动中的listview项中滑动 - Android open listview item in new activity and swipe through the listview items in the activity Android如何在单击ListView项目时隐藏和显示ListView中的项目 - Android How to hide and display items in a ListView when a ListView item is clicked 如何在创建新活动时记住单个ListView项的颜色 - How to remember a color of single ListView item when new activity is created 如何从列表视图中选择单个项目并显示另一个活动中行的结果名称 - How to select single item from listview and display resulted name of row in another activity 如何从外部文本文件(在服务器上)读入一行以显示在Android活动中? - How do I read in a single line from an external text file (on server) to display in Android activity?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM