[英]How can i get the info of the cardview i click on to send it to the next activity
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. 我正在制作一个应用程序,以显示使用recyclerview的cardview列表,并且希望能够对用户单击的cardview进行详细的活动,但是我不知道如何将被单击的项目发送到下一个活动。
Here u can see my adapter: 在这里您可以看到我的适配器:
/** * Created by alberto on 18/05/16. / ** *由alberto在16年5月18日创建。 */
* /
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());
}
}
Put the items as extra to the intent for launching the DetailedActivty: 将其他项目放在启动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);
}
Change your onCreateViewHolder like this 像这样更改您的onCreateViewHolder
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. 您也可以在
onBindView()
方法中执行此onBindView()
。 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
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.