0

I have two options in the list view(Part 1& Part 2). I want each of them to play a different video. (from Youtube i.e). how do I do that? IF statement or is there another way? this is Mainactivity2.

public class Main2Activity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);
    final String[] sublectures = {"Part 1","Part 2"};
    ListAdapter appadapter1 = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, sublectures);
    ListView list2 = findViewById(R.id.list2);
    list2.setAdapter(appadapter1);

    list2.setOnItemClickListener(
            new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    String lecture = String.valueOf(parent.getItemAtPosition(position));
                    Intent startintent = new Intent(getApplicationContext(),Main3Activity.class);
                    startActivity(startintent);

                }


            });
}}

This takes to the Mainactivity 3. i.e

public class Main3Activity extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener {

public static final String API_KEY = "<key here>";
public static final String VIDEO_ID = "W2TYS_Jvzjc";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main3);
    YouTubePlayerView youTubePlayerView = findViewById(R.id.youtubePlayerView);
    youTubePlayerView.initialize(API_KEY, this);
}

@Override

public void onInitializationFailure (YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult){
    Toast.makeText(this, "Fail to Load", Toast.LENGTH_LONG).show();

    }
    @Override
    public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player, boolean wasRestored) {


        if (!wasRestored) {
            player.setPlayerStyle(YouTubePlayer.PlayerStyle.DEFAULT);
            player.cueVideo(VIDEO_ID);
        }
    }
}
Zoe
  • 27,060
  • 21
  • 118
  • 148
  • When you posted the question, you included the YouTube API key. API keys can likely get abused. While it can be removed, a moderator needs to redact the revision (meaning hiding the token entirely along with effectively destroying the revision). But since you've already posted it, there's a chance a web scraper has picked it up, so I highly recommend, if possible, that you change the API key. – Zoe Mar 10 '19 at 20:18

1 Answers1

0

Instead of sublectures being an Array of Strings, create a custom class called Sublecture. This class stores the name of the lecture (Part 1 or Part 2) as well as the URL of the corresponding YouTube video. Sublecture is literally just a class of getters and setters. Then, instead of ArrayAdapter<String>, create a custom ArrayAdapter <Sublecture>. See here:

How to use ArrayAdapter<myClass>

What I'm describing is actually one of the most common things you'll be doing in Android, so take some time and learn how to do it.

Gavin Wright
  • 3,124
  • 3
  • 14
  • 35
  • I created the Custom adapter but how do I set the 2 different URLs for the two list items??? – Muhammad Hassaan Mar 12 '19 at 21:21
  • I'm guessing you mean that you have a ListView with lots of sublectures. And each sublecture has two videos. The Sublecture class should hold the values of the two URLs. The adapter should inflate a list item layout consisting of at least two buttons (one for each video). Then, in the adapter's getView() method, you can set it up so that when button1 is pressed it takes you to video 1 for that sublecture and button2 takes you to video2 of that sublecture. – Gavin Wright Mar 12 '19 at 23:20
  • @Gaving Wright. ya, You are right about that. But I don't know I should I put it in code. I created a custom Adapter, then hit a wall. Its just that i am a beginner at this stuff & struggling a bit. – Muhammad Hassaan Mar 13 '19 at 20:38