I have this ListviewAdapter and I would like to know how I can add a similar button for each element of the list, that when pressed it directs me 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 in other similar questions in the forum but I have not been able to solve it. I suspect that I have to put these instructions, but I do not know where and if it is really like that:
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);
}
}
);
What you do is correct to open another Activity, but in this case, the specified problem is
indicates that within your view you have specified the method,
but in your code it doesn't actually exist:
no button is needed for that, there is a method for the listView to do that:
at first it may show you an error, but you must add this code so that it no longer marks it:
finally, add this:
I hope it helps you.