java - ListView 中的新 Activity

标签 java android arrays android-activity android-studio

我正在开发一个乐谱应用程序,并且 ListView 中有一堆歌曲。 .

我正在使用 onItemClick方法,但问题是,我不知道如何根据选择的子项来打开 Activity 。

歌曲数组为 uta[] ,这样我就可以用 uta[position] 找到特定的字符串,但是如何根据用户在 ListView 中选择的位置打开特定 Activity ?

最佳答案

您可以在使用 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;
    }
}

注意:字符串上的 switch/case 语句需要 JDK 7,请参阅 Oracle documentation .

关于java - ListView 中的新 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28658820/

相关文章:

java - 是否可以中断 Java RMI 调用?

Java彩票使用数组

java - 需要使用 Jdbc 程序访问 Hive 元数据表

android - 任务 'app:mergeDebugResources' Crunching Cruncher 执行失败....png 失败

java - 基于 LinearLayout 创建自定义组件,在 XML 中声明布局

javascript - 如何从数组中删除具有相同值的项目

java - 语言翻译API

android - 如何在 gingerbread 和更低版本的设备上运行 api 级别 14 演示

c - 为什么我的数组在作为参数传递时被删除?

arrays - 是否有转置 3D 数组的 Fortran 函数?