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. I got already the activities stringed to java classes.

Here is my code:

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>

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!

Upvotes: 0

Views: 160

Answers (2)

Pragnani
Pragnani

Reputation: 20155

When you are inside anonymous inner class this will not refer to your current activity class.

You should use MainActivity.this instead of this

i.e

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

Upvotes: 2

Nabil Sunesara
Nabil Sunesara

Reputation: 85

Change this line from

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

to

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

Upvotes: 0

Related Questions