简体   繁体   English

我如何获取我单击的Cardview的信息以将其发送到下一个活动

[英]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.

相关问题 单击 Cardview 时如何在另一个活动中显示详细信息 - How can I display the details in another activity when Cardview is clicked 如何向CardView添加单击侦听器? - How do I add a click listener to a CardView? 如何捕获多个图像并将图像发送到下一个活动并使用 CameraX [Android Studio] 显示它们 - How Can I Capture Multiple Images and send the images to next Activity and display them using CameraX [Android Studio] 在进行下一个活动之前,我无法绘制UI - I can't get the UI to draw before advancing to next activity 如何在 CardView 中添加 Horizo​​ntalScroll View - How can i add HorizontalScroll View in CardView 如何登录Facebook并获取用户信息以从Android应用程序发送我的远程数据库 - How can I login with facebook and get user info to send my remote db from android application 如何获取方法继承信息? - How can I get the method inheritance info? 尽管使用了共享首选项,但如何保存我的 cardView 单击事件状态以保存其颜色 - How can I save my cardView click event state to save its colour in spite of using shared preferences 我怎样才能获得其他活动的背景? - How can I get the context of other activity? 我如何获得开始活动的背景 - How i can get the context of started activity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM