简体   繁体   English

ListView的新活动

[英]New Activity from ListView

I'm working on a music sheet app, and have a bunch of songs in a ListView . 我正在开发一个音乐工作表应用,并且ListView有一堆歌曲。

I'm using an onItemClick method, but the problem is, I don't know how to open an activity depending on what subitem is selected. 我正在使用onItemClick方法,但是问题是,我不知道如何根据选择的子项目打开活动。

The array of songs is uta[] , so I can find the specific String with uta[position] , but how can I open a specific activity based off of the position that is picked by the user in the ListView ? 歌曲数组是uta[] ,因此我可以使用uta[position]找到特定的String,但是如何根据用户在ListView选择的位置来打开特定的活动呢?

You can make a switch/case statement on the String that you fetch with uta[position] 您可以在通过uta[position]获取的字符串上进行switch / case语句

public void onItemClick(AdapterView parent, View v, int position, long id) {
    String value = uta[position].getValue();
    switch(value){
        case "value1":
            Intent intent = new Intent(this, activity1.class); startActivity(intent);
        break;

        case "value2":
            Intent intent = new Intent(this, activity2.class); startActivity(intent);
        break;

        case "value3":
            Intent intent = new Intent(this, activity3.class); startActivity(intent);
        break;
    }
}

Note: switch/case statement on Strings requires JDK 7, see the Oracle documentation . 注意:关于String的switch / case语句需要JDK 7,请参阅Oracle文档

Try in this manner in your onitemclick method 在您的onitemclick方法中以这种方式尝试

    if (position == 0) { 
    Intent intent = new Intent(this, activity1.class);
     startActivity(intent); 
    } else if (position == 1) { 
    Intent intent = new Intent(this, activity2.class);
     startActivity(intent); 
    } else if (position == 2){
     Intent intent = new Intent(this, activity3.class);
 startActivity(intent);
     } 

Your ListView item-click listener provides the view that has been clicked: 您的ListView项目单击侦听器提供已单击的视图:

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Song song = (Song) getItemAtPosition(position);
        if (song.getId() == SOME_ID)
            startActivity(new Intent(this, SomeActivity.class));
        // you can then create the logic for all your activities
    }
});

Also, you may pass the song to one activity that would load itself based on the song data. 另外,您可以将歌曲传递到一项活动,该活动将根据歌曲数据进行加载。 For that, I recommend you read about the Parcelable interface and how to put Extras in an Intent . 为此,我建议您阅读有关Parcelable接口的信息,以及如何将Extras放入Intent

Can you use: 您可以使用:

listView1.setOnItemClickListener(new AdapterView.onItemClickListener() {
   @Override
   public void onItemClick(AdapterView adapter, View view, int position, long arg) {

      switch (position) {
          case 0:
              startActivity(new Intent(this, first_activity.class));
              break;
          case 1:
              startActivity(new Intent(this, second_activity.class));
              break;
         // and more...
      } 
});

Cheers! 干杯!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM