Reputation: 49
I'm making an application were i show a list of cardviews using a recyclerview and i want to be able to make a detail activity of the cardview the user clicks but i dont know how to send the item that is clicked to the next activity.
Here u can see my adapter:
/** * Created by alberto on 18/05/16. */
public class EmpresasAdapter extends RecyclerView.Adapter<EmpresasAdapter.EmpresasViewHolder>
implements ItemClickListener{
private final Context context;
private List<Empresas> items;
public EmpresasAdapter(Context context, List<Empresas> items) {
this.context = context;
this.items = items;
}
@Override
public int getItemCount() {
return items.size();
}
@Override
public EmpresasViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.empresa_card, viewGroup, false);
return new EmpresasViewHolder(v, this);
}
@Override
public void onBindViewHolder(EmpresasViewHolder viewHolder, int i) {
// Item procesado actualmente
Empresas currentItem = items.get(i);
viewHolder.nombrep.setText(currentItem.getNombre());
viewHolder.descripcionp.setText(currentItem.getDescripcionC());
viewHolder.franquiciap.setText(currentItem.getModalidad()+" | "+currentItem.getFranquicia());
// Cargar imagen
Glide.with(context)
.load(currentItem.getImagen())
.into(viewHolder.imagenp);
}
@Override
public void onItemClick(View view, int position) {
// Imagen a compartir entre transiciones
View sharedImage = view.findViewById(R.id.imagenp);
EmpresaDetalle.launch(
(Activity) context, position, sharedImage);
}
/**
* View holder para reciclar elementos
*/
public static class EmpresasViewHolder extends RecyclerView.ViewHolder
implements View.OnClickListener {
// Views para un curso
public ImageView imagenp;
public TextView nombrep;
public TextView descripcionp;
public TextView franquiciap;
// Interfaz de comunicación
public ItemClickListener listener;
public EmpresasViewHolder(View v, ItemClickListener listener) {
super(v);
nombrep = (TextView) v.findViewById(R.id.nombrep);
descripcionp = (TextView) v.findViewById(R.id.descripcionp);
franquiciap = (TextView) v.findViewById(R.id.Franquiciap);
imagenp = (ImageView) v.findViewById(R.id.imagenp);
v.setOnClickListener(this);
this.listener = listener;
}
@Override
public void onClick(View v) {
listener.onItemClick(v, getAdapterPosition());
}
}
Upvotes: 0
Views: 814
Reputation: 762
Change your onCreateViewHolder like this
public EmpresasViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.empresa_card, viewGroup, false);
v.setOnClickListener(new View.OnClickListener{
@Override
public void onClick(View v){
//you have the cardView in v;
//you can access the child view of your cardView by referring to v
//if you want to get the position you can do it here.
//Create an intent with the collected data an d start the activity
}
});
return new EmpresasViewHolder(v, this);
}
You can do this in onBindView()
method too. Position is easily accessible with this method. But that does not mean that you can't have the position in the former method. You can. But I've not described it. Will do if you want.
//to do this its easier if you have assigned and id to the cardView in XML and store the cardView in the viewHOlder
//getting cardView
CardView cardView=holder.cardView;
cardView.setOnClickListener(new View.OnClickListener{
@Override
public void onClick(View v){
//position is easily availble now.
//you can access the rest of the views
//create youtr intent and start the activity
}
});
Upvotes: 1
Reputation: 111
Put the items as extra to the intent for launching the DetailedActivty:
@Override
public void onItemClick(View view, int position) {
// Imagen a compartir entre transiciones
View sharedImage = view.findViewById(R.id.imagenp);
Intent intent = new Intent(context, DetailedActivty.class)
intent.putExtra(nombrep, NOMBREP_EXTRA);
intent.putExtra(descripcionp, DESCRIPCIONP_EXTRA);
intent.putExtra(franquiciap, FRANQUICIAP_EXTRA);
context.startActivity(intent);
}
Upvotes: 1