views:

131

answers:

2

I have a ListView that shows a list of names. When you select a name, I want to pass the selected person' ID to the next view (Profile) and retreieve their data based on their ID.

I am able to load the Profile View, but do not know how to pass the ID from the ListView to the Profile. Here is how I am loading the Profile:

    lv.setOnItemClickListener(new OnItemClickListener() {   
        public void onItemClick(AdapterView<?> parent, View view,int position, long id) {      
             Intent myIntent = new Intent(view.getContext(), SubView.class); // when a row is tapped, load SubView.class
             startActivityForResult(myIntent, 0); // display SubView.class                  
        }
    }); 
A: 

An intent includes a bundle of extras:

Intent myIntent = new Intent(view.getContext(), SubView.class); 
myIntent.putExtra("id", id);
startActivityForResult(myIntent, 0); // display SubView.class         

In the oncreate method of your profile activity you can access the extras:

int id = getIntent().getIntExtra("id");         
Mayra
definitely need to use getIntExtra or getStringExtra.
Chris
A: 

First Activity

Intent myIntent = new Intent();
myIntent.putExtra("key", "value");
startActivity(myIntent); 

New Activity

Intent myIntent = getIntent(); // this is just for example purpose
myIntent.getExtra("key");
Pentium10