如何将数据从RecyclerView传递给活动

如何将数据从RecyclerView传递给活动

本文介绍了如何将数据从RecyclerView传递给活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将数据从 recyclerView adapter 传递到 main 活动点击recyclerview的图片。有人可以帮忙吗?

I need to pass data from recyclerView adapter to main activity on click on image of recyclerview. Can someone help?

public  class VideoAdapter extends RecyclerView.Adapter<VideoAdapter.VideoHolder> {
    private ArrayList<Video> mData;
    private ArrayList<Video> mData2;


    private Activity mACtivity;

    public VideoAdapter(ArrayList<Video> data, ArrayList<Video> data2, Activity activity) {
        this.mData = data;
        this.mData2 = data2;

        this.mACtivity = activity;
    }


    @Override
    public VideoHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.anteprima_list_item, parent, false);
        //view.setOnClickListener(mOnClickListener);

        return new VideoHolder(view);
    }

    @Override
    public void onBindViewHolder(VideoHolder holder, int position) {
        Video video = mData.get(position);
        final Video video2 = mData2.get(position);

        holder.setTitolo(video.getTitolo());
        holder.setSottoTitolo(video.getSottotitolo());

        holder.setData(video.getData());

        holder.setData(video.getData());


       /* holder.setAddress(restaurant.getAddress());
        holder.setCost("Average cost for 2: " + restaurant.getCurrency() + restaurant.getCost());
        holder.setRating(restaurant.getRating());*/

        Glide.with(mACtivity)
                .load(video2.getPic())
                .into(holder.restaurantImageView);

        holder.restaurantImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // handle click event here
                System.out.println("PIC"+video2.getPic());
            }
        });

    }

    @Override
    public int getItemCount() {
        if (mData == null)
            return 0;
        return mData.size();
    }

    public class VideoHolder extends RecyclerView.ViewHolder {

        ImageView restaurantImageView;
        TextView restaurantNameTextView;
        TextView restaurantAddressTextView;
        TextView restaurantRatingTextView;
        TextView costTextView;
        TextView distanceTextView;
        LinearLayout linearlayout;

        public VideoHolder(View itemView) {
            super(itemView);
             linearlayout=(LinearLayout) itemView.findViewById((R.id.linearlayout));

            restaurantImageView = (ImageView) itemView.findViewById(R.id.imageview_restaurant);
            restaurantNameTextView = (TextView) itemView.findViewById(R.id.textview_restaurant_name);
            restaurantAddressTextView = (TextView) itemView.findViewById(R.id.restaurant_address_textview);
            distanceTextView = (TextView) itemView.findViewById(R.id.restaurant_distance_textview);
           /*  costTextView = (TextView) itemView.findViewById(R.id.cost_for_two_textview);

           restaurantRatingTextView = (TextView) itemView.findViewById(R.id.rating);*/


        }


        public void setTitolo(String titolo) {
            restaurantNameTextView.setText(titolo);
        }

        public void setSottoTitolo(String sottotitolo) {
            restaurantAddressTextView.setText(sottotitolo);
        }

        public void setData(String data) {
            distanceTextView.setText(data);
        }
      /*  public void setPic(String pic) {
            distanceTextView.setText(pic);
        }

         public void setCost(String cost) {
            costTextView.setText(cost);
        }

        public void setDistance(String distance) {
            distanceTextView.setText(distance);
        }*/
    }
}


推荐答案

创建一个监听器接口,让MainActivity实现它。这样你就可以在你的onClick方法中调用你的回调方法。

Create a listener Interface and let your MainActivity implement it. That way you can call your callback method in your onClick method.

接口:

public interface OnImageClickListener {
    void onImageClick(String imageData);
}

MainActivity:

MainActivity:

public class MainActivity implements OnImageClickListener {
    @Override
    public void onImageClick(String imageData) {
        // handle image data
    }
//...
}

您的VideoAdapter:

Your VideoAdapter:

    //...
    private OnImageClickListener onImageClickListener;

    public VideoAdapter(ArrayList<Video> data, ArrayList<Video> data2, Activity activity, OnImageClickListener onImageClickListener) {
        this.mData = data;
        this.mData2 = data2;
        this.mACtivity = activity;
        this.onImageClickListener = onImageClickListener;
    }
    //...
    @Override
    public void onBindViewHolder(VideoHolder holder, int position) {
        //...
        holder.restaurantImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onImageClickListener.onImageClick(video2.getPic());
            }
        });
        //...
    }
    //...

这篇关于如何将数据从RecyclerView传递给活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 11:14