When implementing a navigation drawer and listview, the navigation drawer does not perform any action

2

I try to implement a navigation drawer and a listview in the same activity, but the problem is that only lisview (listview1) works for me.

The listview of the navigation drawer (listview2) does not work: the menu is displayed but does not execute any action .

Apparently the listView2.setOnItemClickListener method is not executed.

How can I make it work?

MainActivity class

public class MainActivity extends AppCompatActivity {

private String[] opciones = new String[] {"opcion1", "opcion2", "opcion3","opcion4"};
private DrawerLayout drawerLayout;
private ListView listView1;
private ListView listView2;
private ActionBarDrawerToggle drawerToggle;
int posicion;



private Productos[] datos = new Productos[]{
        new Productos(R.drawable.hamburguesa,20000,"Hamburguesa","Con papas y gaseosa"),
        new Productos(R.drawable.perro,12000,"Perro","Con tocineta"),
        new Productos(R.drawable.chusos,15000,"Chuzo","De res o de pollo"),
        new Productos(R.drawable.jugo,6000,"Jugo","En agua o en leche")
};

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

    Adapter adaptador = new Adapter(this, datos);

    listView2 = (ListView) findViewById(R.id.listView);


    listView2.setAdapter(adaptador);

    listView2.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            Toast.makeText(getApplicationContext(),"presiono "+i,Toast.LENGTH_SHORT).show();
            Log.d("producto presionado", ((Productos)adapterView.getItemAtPosition(i)).getNombre());
        }
    });



    ActionBar actionBar = getSupportActionBar();
    if (actionBar != null){
        actionBar.setHomeAsUpIndicator(R.drawable.ic_menu_white_24dp);
        actionBar.setDisplayHomeAsUpEnabled(true);
    }

    drawerLayout = (DrawerLayout) findViewById(R.id.contenedorPrincipal);
    listView1 = (ListView) findViewById(R.id.menuIzq);

    listView1.setAdapter(new ArrayAdapter<String>(getSupportActionBar().getThemedContext(),
            android.R.layout.simple_list_item_1, opciones));

    listView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

            switch (i){
                case(0):
                    Toast.makeText(MainActivity.this, "presiono 1",Toast.LENGTH_SHORT).show();
                    break;
                case(1):
                    Toast.makeText(MainActivity.this, "presiono 2",Toast.LENGTH_SHORT).show();
                    break;
                case(2):
                    Toast.makeText(MainActivity.this, "presiono 3",Toast.LENGTH_SHORT).show();
                    break;
                case(3):
                    Toast.makeText(MainActivity.this, "presiono 4",Toast.LENGTH_SHORT).show();
                    break;
            }

            listView1.setItemChecked(i,true);
            drawerLayout.closeDrawer(listView1);
        }
    });

    drawerToggle = new ActionBarDrawerToggle(this,drawerLayout,R.string.abierto, R.string.cerrado);

    drawerLayout.setDrawerListener(drawerToggle);



}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()){
        case android.R.id.home:
            drawerLayout.openDrawer(Gravity.LEFT);
            return true;
    }

    return super.onOptionsItemSelected(item);
}




class Adapter extends ArrayAdapter<Productos> {
    public Adapter(Context context, Productos[] datos) {
        super(context, R.layout.producto_item, datos);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {


        LayoutInflater inflater = LayoutInflater.from(getContext());
        View item = inflater.inflate(R.layout.producto_item, null);

        ImageView imagen = (ImageView) item.findViewById(R.id.iImagen);
        imagen.setImageResource(datos[position].getIdImagen());

        TextView nombre = (TextView) item.findViewById(R.id.tNombre);
        nombre.setText(datos[position].getNombre());
        TextView descripcion = (TextView) item.findViewById(R.id.tDescripcion);
        descripcion.setText(datos[position].getDescripcion());
        TextView precio = (TextView) item.findViewById(R.id.tPrecio);
        precio.setText(String.valueOf(datos[position].getPrecio()));


        return (item);
    }
}

}

MainActivity Layout

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"

android:id="@+id/contenedorPrincipal"
tools:context=".MainActivity">


<ListView
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:id="@+id/menuIzq"
    android:choiceMode="singleChoice"
    android:foregroundGravity="left"
    android:layout_gravity="start">
</ListView>


<ListView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/listView">
</ListView>


</android.support.v4.widget.DrawerLayout>

Products class

public class Productos {

int idImagen, precio;
String nombre, descripcion;

public Productos(int idImagen, int precio, String nombre, String descripcion) {
    this.idImagen = idImagen;
    this.precio = precio;
    this.nombre = nombre;
    this.descripcion = descripcion;
}

public String getDescripcion() {
    return descripcion;
}

public void setDescripcion(String descripcion) {
    this.descripcion = descripcion;
}

public String getNombre() {
    return nombre;
}

public void setNombre(String nombre) {
    this.nombre = nombre;
}

public int getPrecio() {
    return precio;
}

public void setPrecio(int precio) {
    this.precio = precio;
}

public int getIdImagen() {
    return idImagen;
}

public void setIdImagen(int idImagen) {
    this.idImagen = idImagen;
}
}

Layout Products

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="120dp">

<ImageView
    android:layout_width="120dp"
    android:layout_height="120dp"
    android:id="@+id/iImagen"
    android:src="@drawable/hamburguesa"/>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="25sp"
        android:text="Hamburguesa"
        android:id="@+id/tNombre"
        android:textColor="@color/black"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:text="Con papas y gaseosa"
        android:id="@+id/tDescripcion"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="25sp"
        android:text="20000"
        android:id="@+id/tPrecio"
        android:textColor="@color/red"
        android:gravity="right"/>

</LinearLayout>

</LinearLayout>
    
asked by Vash 09.10.2016 в 07:10
source

1 answer

0

Add in your ListView the property android:choiceMode="singleChoice" , with that it should work:

<ListView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:choiceMode="singleChoice"
    android:id="@+id/listView">
</ListView>
    
answered by 10.10.2016 в 18:39