Open Activity according to the Item selected in ListView [closed]

1

How to make an item in the Android listview open a "different activity" according to the selected item?

I have declared these variables in the java file, but it flags me in red adapterView

package com.example.juan.pruebaprueba;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends AppCompatActivity {

ListView listviewmodelo;
String[] listviewlista = new String[] {
        "Elemento 100",
        "Elemento 200",
        "Elemento 300",
        "Elemento 400",
        "Elemento 500",
        "Elemento 600"
};
List<String> convertString;
ArrayAdapter<String> arrayadapter;

@Override

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    listviewmodelo = (ListView)findViewById(R.id.list);

    convertString = new ArrayList<String>(Arrays.asList(listviewlista));

    arrayadapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, convertString);


    listviewmodelo.setAdapter(arrayadapter);

// Esta parte me ayudaron en StackoverFlow


listviewFontChange.setOnItemClickListener(
                new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                    // Abre una nueva Activity:
                    Intent myIntent = new Intent(view.getContext(), nuevaactivity.class);
                    startActivity(myIntent);

                }
            }
    );


}


}
    
asked by Juan Moreno 18.04.2017 в 18:08
source

1 answer

0

First you are getting the reference of a ListView within the layout activity_main.xml and it is listviewmodelo :

listviewmodelo = (ListView)findViewById(R.id.list);

therefore you must use that instance to configure the listener, and not the listviewFontChange :

  //listviewFontChange.setOnItemClickListener(
  listviewmodelo.setOnItemClickListener(

According to the selected item it can be defined by the value of position :

listviewmodelo.setOnItemClickListener(
                new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                    if (position == 0){
                    // Abre una nueva Activity:
                    Intent myIntent = new Intent(view.getContext(), nuevaActivity.class);
                    startActivity(myIntent);
                    }else if(position == 1){
                    // Abre una nueva Activity:
                    Intent myIntent = new Intent(view.getContext(), segundaactivity.class);
                   }else if(position == 2){
                    // Abre una nueva Activity:
                    Intent myIntent = new Intent(view.getContext(), terceraActivity.class); 
                   } 


                }
            }
    );
    
answered by 18.04.2017 в 19:08