开发者

Android - Passing Value from ListView to a new Activity?

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜