【问题标题】:Android Studio - How to open specific activities from a ListView?Android Studio - 如何从 ListView 打开特定活动?
【发布时间】:2016-06-20 06:42:15
【问题描述】:

我在完成代码以从我制作的列表中打开活动时遇到问题。我正在制作一个方程式应用程序,我想要一个主题列表,当您单击一个时,它会启动包含我已经拥有的方程式的 .xml 文件。我已经将活动与 java 类联系起来。

这是我的代码:

MainActivity.java:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_principal);

    //get list view from xml
    temasListView = (ListView) findViewById(R.id.temasListView);


    String[] Temas = {
            "Conversion",
            "Suma",
            "Trigonometria",
            "Primera",
            "Momento",
            "Centro",
            "Segunda1",
            "MRU",
            "MRUA",
            "Tiro",
            "Segunda2"};

    ListAdapter temasAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, Temas);
    ListView temasListView = (ListView) findViewById(R.id.temasListView);
    temasListView.setAdapter(temasAdapter);

    temasListView.setOnItemClickListener(
            new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    String temas = String.valueOf(parent.getItemAtPosition(position));
                    Toast.makeText(Temas.this, temas, Toast.LENGTH_LONG).show();

                    if (position == 0) {
                        Intent intent = new Intent(this, Conversion.class);
                        startActivity(intent);
                    }
                    else if (position == 1) {
                        Intent intent = new Intent(this, Suma.class);
                        startActivity(intent);
                        }
                    else if (position == 2) {
                        Intent intent = new Intent(this, Trigonometria.class);
                        startActivity(intent);
                    }
                    else if (position == 3) {
                        Intent intent = new Intent(this, Primera.class);
                        startActivity(intent);}

                    else if (position == 4) {
                        Intent intent = new Intent(this, Momento.class);
                        startActivity(intent);
                    }
                    else if (position == 5) {
                        Intent intent = new Intent(this, Centro.class);
                        startActivity(intent);
                    }
                    else if (position == 6) {
                        Intent intent = new Intent(this, Segunda1.class);
                        startActivity(intent);
                    }

                    else if (position == 7) {
                        Intent intent = new Intent(this, MRU.class);
                        startActivity(intent);
                    }
                    else if (position == 8) {
                        Intent intent = new Intent(this, MRUA.class);
                        startActivity(intent);
                    }
                    else if (position == 9) {
                        Intent intent = new Intent(this, Tiro.class);
                        startActivity(intent);
                    }
                    else if (position == 10) {
                        Intent intent = new Intent(this, Segunda2.class);
                        startActivity(intent);
                    }




            });
}

strings.xml:

<resources>
<string name="app_name"></string>

<string-array name="temas">
    <item>Conversión de Unidades</item>
    <item>Suma de Vectores</item>
    <item>Trigonometría</item>
    <item>Primera Ley de Newton</item>
    <item>Momento de Fuerzas</item>
    <item>Centro de Gravedad</item>
    <item>Componente de Velocidad</item>
    <item>Segunda Ley de Newton</item>
    <item>Movimiento Rectilíneo Uniforme</item>
    <item>MRUA</item>
    <item>Tiro Vertical</item>
    <item>Segunda Ley de Newton (DCL)</item>


</string-array>

还有我的主要活动:

<LinearLayout   
<ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:entries="@array/temas"
    android:id="@+id/temasListView"
    android:layout_weight="1.05"
    android:background="#dadada" />

</LinearLayout>

我需要帮助来完成这项工作!

【问题讨论】:

  • 具体是什么问题?
  • 请注意,您在 Android Studio 中编写代码,它不是启动活动,您的问题与 Android Studio 无关。
  • 抱歉,问题是我不知道如何使用 ListView 启动活动。我尝试了代码,但它标记了很多错误。

标签: java android listview android-studio


【解决方案1】:

更改此行
Toast.makeText(Temas.this, temas, Toast.LENGTH_LONG).show();

Toast.makeText(MainActivity.this, temas, Toast.LENGTH_LONG).show();

【讨论】:

    【解决方案2】:

    当您在匿名内部类中时,this 不会引用您当前的活动类。

    您应该使用MainActivity.this 而不是this

    Intent intent = new Intent(MainActivity.this, Conversion.class);
    startActivity(intent);
    

    你可以像这样重构你的代码,去掉 switch case。

    String className= parent.getItemAtPosition(position).toString();
    Class myClass=Class.forName("yourpackagename"+className);
    Intent intent = new Intent(MainActivity.this, myClass);
    startActivity(intent);
    

    不需要使用很多开关条件。

    同样如上述答案中所指出的,在您的 toast 消息中使用 MainActivity.this instad of Temas.this

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-06-10
      • 1970-01-01
      • 2022-11-03
      • 1970-01-01
      • 2018-10-08
      • 2018-05-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多