I´m tryng to start a new activity when the user clicks on a RecycleView element . The problem is that only one field of object I want to pass to JSON is show in the row_item Layout and don´t know how to retrieve the other 3 object fields.
Here are the steps I´m following .
First I send a list of objects (Poliza) to the adapter . Second I show only one field of the object (poliza) in the RecycleView . Third I want to start a new activity when the user clicks on the RecycleView item , sending a JSON of the object with the other object fields that are not show in the row_item .
Here is my adapter
public class PolizasAdapter extends RecyclerView.Adapter<PolizasAdapter.PolizaViewHolder> {
private List<Poliza> listapoliza = null;
private Context context;
public PolizasAdapter( List<Poliza> listapoliza) {
this.listapoliza = listapoliza;
}
public class PolizaViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
// each data item is just a string in this case
TextView txtPoliza;
public PolizaViewHolder(View view) {
super(view);
//Agregamos un onclickListener
view.setOnClickListener(this);
context = view.getContext();
//Hacemos referencia a las vistas del rowitem
txtPoliza = (TextView) view.findViewById(R.id.txt_poliza);
}
@Override
public void onClick(View view) {
//Evento que que se genera cuando se da click
TextView texto = (TextView) view.findViewById(R.id.txt_poliza);
String str = texto.getText().toString();
System.out.println(str);
//Iniciamos actividad mostrando el detalle
Intent intent = new Intent(context , DetalleActivity.class);
intent.putExtra("id_poliza",str);
//Whats next ?
//How to add the other 3 fields of my object to the putExtra
}
}
@Override
public PolizasAdapter.PolizaViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//Creamos una nueva vista
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_item, parent, false);
PolizaViewHolder viewHolder = new PolizaViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(PolizasAdapter.PolizaViewHolder holder, int position) {
holder.txtPoliza.setText("Poliza : " + String.valueOf(listapoliza.get(position).getPoliza()));
}
)
@Override
public int getItemCount() {
return listapoliza.size();
}
}
Update the ViewHolder to take in onClick() as a parameter. In the initializer, call setOnClickListener{} on the itemView . That's it! Your RecyclerView is now responsive so time to get your click on!
In your ViewHolder class in your RecyclerView adapter, implement View. OnClickListener, bind the listener to the view. In the onClick method, call the onClick method of the interface OnItemClickListener. This should be passed in from your RecycyclerView's constructor.
For starting the Activity you should do context.startActivity(intent);
As for the 3 JSON parameter you're talking about, i couldn't find any reference of them in your code (there's no reference to any JSON whatsoever). I guess you might be talking about a field from Poliza object, but i can't be sure.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With