gpt4 book ai didi

java - 如何将数据从 RecyclerView 传递到 Activity

转载 作者:太空狗 更新时间:2023-10-29 15:31:09 24 4
gpt4 key购买 nike

我需要在点击 recyclerview 图像时将数据从 recyclerView adapter 传递到 main activity。有人可以帮忙吗?

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);
}*/
}
}

最佳答案

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

接口(interface):

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

主要 Activity :

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

您的视频适配器:

    //...
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());
}
});
//...
}
//...

关于java - 如何将数据从 RecyclerView 传递到 Activity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47182944/

24 4 0
文章推荐: javascript - 如何使用 jQuery 将