开发者

Android - Passing Value from ListView to a new Activity?

开发者 https://www.devze.com 2023-01-03 05:15 出处:网络
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 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 Lis开发者_如何转开发tView 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                  
        }
    }); 


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


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");
0

精彩评论

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