将If语句添加到列表视图还是有其他方法?

时间:2019-03-10 20:11:45

标签: android android-intent android-youtube-api

我在列表视图中有两个选项(第1部分和第2部分)。我希望他们每个人都播放不同的视频。 (来自YouTube)。我怎么做? IF语句还是还有其他方法?这是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);

                }


            });
}}

这涉及Mainactivity3。即

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

1 个答案:

答案 0 :(得分:0)

创建一个名为sublectures的自定义类,而不是Sublecture是一个字符串数组。此类存储讲座的名称(第1部分或第2部分)以及相应的YouTube视频的URL。 Sublecture实际上只是一类getter和setter。然后,创建一个自定义ArrayAdapter<String>而不是ArrayAdapter <Sublecture>。看到这里:

How to use ArrayAdapter<myClass>

我所描述的实际上是您将要在Android中执行的最常见的事情之一,所以花一些时间来学习如何做。