开发者

How can i start a new activity within an Activity?

开发者 https://www.devze.com 2023-03-18 07:47 出处:网络
For example i have a video开发者_如何学JAVA gallery. I scroll to a certain video click on it and a whole new activity starts. Whats the most efficient way to code this?If you have a list, I\'m sure yo

For example i have a video开发者_如何学JAVA gallery. I scroll to a certain video click on it and a whole new activity starts. Whats the most efficient way to code this?


If you have a list, I'm sure you are getting a hold of it in the code and assigning an Adapter. You can define what happens (starting an activity) by defining setOnItemClickListener() as such:

final ListView list = (ListView)findViewById(R.id.myVideoList);
MyVideoAdapter adapter = new MyVideAdapter(this);
list.setAdapter(adapter);
list.setOnItemClickListener(new ListView.OnItemClickListener(){
    public void onItemClick(AdapterView<?> av, View v, int position, long id){
        MyVideoObject video = (MyVideoObject)av.getItemAtPosition(position);
        Intent intent = new Intent(CurrentActivity.this, SelectedVideo.class);
        intent.putExtra("video_id, video.getID());
        startActivity(intent);
    }
});

All you're doing above is to initializing the list and assigning what happens when the user clicks on the list item. When a click happens, you're getting a hold of the video item (however you're juggling them between activities) and passing perhaps its ID to the new Activity you wanna launch. The new activity (SelectedVideo.class) can receive the ID in onCreate and perhaps play the video.

Hope this helps,

-serkan

0

精彩评论

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

关注公众号