Click on a button in a listview to have an activity open

2

I have this ListviewAdapter and I would like to know how I can add an equal button to each item in the list, that when I press it, I will be directed to another activity.

public class ListViewAdapter extends BaseAdapter {
// Declare Variables
Context context;
String[] titulos;
Bitmap[] imagenes;
LayoutInflater inflater;
String[] plazas;
String[] puertas;
String[] maletas;
String[] aire;
String[] transmision;

public ListViewAdapter(Context context, String[] titulos, Bitmap[] imagenes, String[] plazas,String[] puertas,String[] maletas,String[] aire,String[] transmision) {
    this.context = context;
    this.titulos = titulos;
    this.imagenes = imagenes;
    this.plazas=plazas;
    this.puertas=puertas;
    this.maletas=maletas;
    this.aire=aire;
    this.transmision=transmision;

}

@Override
public int getCount() {
    return titulos.length;
}

@Override
public Object getItem(int position) {
    return null;
}

@Override
public long getItemId(int position) {
    return 0;
}

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

    // Declare Variables
    TextView txtTitle;
    ImageView imgImg;
    TextView txplazas;
    TextView txpuertas;
    TextView txmaletas;
    TextView txaire;
    TextView txtransmision;
    //http://developer.android.com/intl/es/reference/android/view/LayoutInflater.html
    inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View itemView = inflater.inflate(R.layout.list_row, parent, false);

    // Locate the TextViews in listview_item.xml
    txtTitle = (TextView) itemView.findViewById(R.id.list_row_title);
    imgImg = (ImageView) itemView.findViewById(R.id.list_row_image);

    txplazas = (TextView) itemView.findViewById(R.id.tplazas);
    txpuertas = (TextView) itemView.findViewById(R.id.tpuertas);
    txmaletas = (TextView) itemView.findViewById(R.id.tmaletas);
    txaire = (TextView) itemView.findViewById(R.id.taire);
    txtransmision = (TextView) itemView.findViewById(R.id.ttransmision);

    // Capture position and set to the TextViews
    txtTitle.setText(titulos[position]);
    imgImg.setImageBitmap(imagenes[position]);
    txplazas.setText(plazas[position]);
    txpuertas.setText(puertas[position]);
    txmaletas.setText(maletas[position]);
    txaire.setText(aire[position]);
    txtransmision.setText(transmision[position]);

    return itemView;
}
}



public class MainActivity extends AppCompatActivity {

ListViewAdapter adapter;

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


    String[] titulo = new String[]{
            "titulo1",
            "titulo2",
            "titulo3",
            "titulo4",
    };

    Bitmap[] imagenes = {
            BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher),
            BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher),
            BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher),
            BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher)
    };

    String[] plazas = new String[]{
            "plazas",
            "plazas",
            "plazas",
            "plazas",
    };
    String[] puertas = new String[]{
            "puertas",
            "puertas",
            "titulo3",
            "titulo4",
    };
    String[] maletas = new String[]{
            "maletas",
            "titulo2",
            "titulo3",
            "titulo4",
    };
    String[] aire = new String[]{
            "aire",
            "titulo2",
            "titulo3",
            "titulo4",
    };
    String[] transmision = new String[]{
            "transmision",
            "titulo2",
            "titulo3",
            "titulo4",
    };


    final ListView lista = (ListView) findViewById(R.id.listView1);
    adapter = new ListViewAdapter(this, titulo, imagenes,plazas,puertas,maletas,aire,transmision);
    lista.setAdapter(adapter);

}
}

I have read other similar questions in the forum but I have not been able to solve it. I imply that I have to put these instructions, but I do not know where and if it really is:

Button boton;
boton = (Button) itemView.findViewById(R.id.botonact1);

lista.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(), Pago.class);
                    startActivity(myIntent);

                }
            }
    );

These are the errors that logcat shows me

    
asked by javi fer 03.06.2017 в 17:16
source

2 answers

2

What you do is correct to open another Activity, but in this case, the specified problem is

  

IllegalStateException: Could not find method pressed (View) in parent or   ancestor Context for android: onClick attribute defined on view class   android.support

indicates that within your view you have specified the method,

android:onClick=pulsao

but in your code it does not really exist:

public void pulsao(View v){
...
}
    
answered by 03.06.2017 / 19:32
source
2

you do not need a button for that, there is a method for the listView to do that:

implements AdapterView.OnItemClickListener //pon esto en tu clase, con el base adapter

At the beginning you can show an error, but you must add this code so that it no longer marks it:

@Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        //el codigo va aquí
    }

Finally, add this:

TuListView.setOnItemClickListener(this); //se usa this ya que se hace en la misma clase

I hope and it will be helpful.

    
answered by 03.06.2017 в 18:01