开发者

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. Thanks!

I have a ListView of multiple items(messages) that are listed in my NewsActivity as follows:

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. The part I need help with is the last function onListItemClick. The function creates a new RSSItem Activity. 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:

public class RSSItem extends Activity {
private Message rssItem;
private String title;
private String date;
private 开发者_如何学JAVAString 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. 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. However, I am not exactly sure how this works/how to do it. Is it completely determined by the R.layout.rssitem file? Here is my R.layout.rssitem file:

<?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. Any help would be greatly appreciated!!


Usually the easiest thing to do is to add extra's to your Intent before calling startActivity.

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

@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);

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜