1

I've managed to populate an Activity (which we'll call Activity A) with a custom listview which works fine.

When the user clicks on an item from the list of items in Activity A, I want to pass details of that item into Activity B.

This is what my activity A looks like:

public class ChannelList extends Activity implements OnItemClickListener {

    public static final String[] names = new String[] { "BBC One",    "BBC Two" };
    public static final String[] descs = new String[] { "London, SD", "England, SD"};
    ListView listView;
    List<ChannelItem> channelItems;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_channel_list);

        channelItems = new ArrayList<ChannelItem>();
        for (int i = 0; i < names.length; i++) {
            ChannelItem item = new ChannelItem(names[i], descs[i]);
            channelItems.add(item);
        }

        listView = (ListView) findViewById(R.id.listView1);
        ChannelListViewAdapter adapter = new ChannelListViewAdapter(this, R.layout.item_channel_list, channelItems);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(this);      

    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
//      Toast toast = Toast.makeText(getApplicationContext(), "Item " + (position + 1) + ": " + channelItems.get(position), Toast.LENGTH_SHORT);            
//      toast.setGravity(Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 0);
//      toast.show();
        Intent intent = new Intent(this, ScheduleList.class);
        startActivity(intent);
    }


}

Based on my interpretation of the Android API documentation, I'm supposed to pass data into the Activity B call using Intent.putExtra();

Question is, how do I pass the name and description elements of the list item that the user clicks on?

For example, when I call Activity B, it should be able to download TV schedule data for BBC One if BBC One is clicked on from the listview. How do I make Activity B know which listitem click triggered Activity B?

4

1 回答 1

0

怎么样

intent.putExtra("name", names[position]);

ActivityA的onItemClick方法和

String name = getIntent().getStringExtra("name");

ActivityB 的onCreate方法中?

于 2013-11-03T19:33:50.830 回答