简体   繁体   English

Android Studio-如何从ListView打开特定活动?

[英]Android Studio - How to open specific activities from a ListView?

I´ve got problems to finish my code to open activities from a list that I made. 我在完成我的代码以打开我创建的列表中的活动时遇到问题。 I´m making an app of equations and I want to have a list of the subjects and when you click on one, it starts the .xml file that have the equations that I already have. 我正在制作一个方程式应用程序,我想要一个主题列表,当您单击一个主题时,它将启动具有我已经拥有的方程式的.xml文件。 I got already the activities stringed to java classes. 我已经把活动串到了Java类上。

Here is my code: 这是我的代码:

MainActivity.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: 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>

And my principal activity: 我的主要活动是:

<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>

I need help to finish this please! 我需要帮助来完成此操作!

When you are inside anonymous inner class this will not refer to your current activity class. 当你匿名内部类中this将不参考当前的活动类。

You should use MainActivity.this instead of this 您应该使用MainActivity.this而不是this

ie

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

You can refactor your code like this, to get rid of switch case. 您可以像这样重构代码,以摆脱开关的情况。

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

No need to use many switch conditions. 无需使用许多开关条件。

Also as pointed in above answer use MainActivity.this instad of Temas.this in your toast message 同样如上述答案中所述,在您的敬酒消息中使用MainActivity.this instad Temas.this

Change this line from 将此行从

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

to

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

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

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