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

时间:2017-11-08 15:01:15

标签: java android

我需要点击recyclelerview的图片,将数据从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);
        }*/
    }
}

4 个答案:

答案 0 :(得分:8)

如果您想将值从onclick传递到Parent activity,请在onMethodCallback中使用MainActivity

public class MainActivity extends Activity implements AdapterCallback {

    private MyAdapter mMyAdapter;

    @Override
    public void onMethodCallback(String yourValue) {
       // get your value here
    }

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        this.mMyAdapter = new MyAdapter(this);
    }
}

在适配器中:

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {

    private AdapterCallback mAdapterCallback;

    public MyAdapter(Context context) {
        try {
            this.mAdapterCallback = ((AdapterCallback) context);
        } catch (ClassCastException e) {
            throw new ClassCastException("Activity must implement AdapterCallback.");
        }
    }

    @Override
    public void onBindViewHolder(final MyAdapter.ViewHolder viewHolder, final int i) {
        // simple example, call interface here
        // not complete
        viewHolder.itemView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mAdapterCallback.onMethodCallback();
            }
        });
    }

    public static interface AdapterCallback {
        void onMethodCallback(String yourValue);
    }
}

答案 1 :(得分:7)

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

接口:

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

MainActivity:

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

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

答案 2 :(得分:1)

如果您正在使用Kotlin,则解决方案要简单得多。 Original Answer here

将lamda val itemClick: (Int) -> Unit传递给适配器。

class MyRecyclerViewAdapter(params , val itemClick: (Int) -> Unit): RecyclerView.Adapter<RecyclerView.ViewHolder>() {
internal inner class MyViewHolder(view: View) : RecyclerView.ViewHolder(view) {
    ...
    itemView.setOnClickListener( {itemClick(layoutPosition)} )
  } 
}  

在“活动”中,使用返回值position

val myAdapter = MyRecyclerViewAdapter(params) { position ->
    // do something
}

答案 3 :(得分:0)

在你的适配器中写下interface,如下所示:

interface clickItemListener {
    void onItemClick(String order);
}

你的构造函数应该是:

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

然后您可以转到Activity

yourLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    clickItemListeners.onItemClick(yourValue);
                }
            });

implement您的interface活动中的public class yourActivity extends AppCompatActivity implements YourAdapter.clickItemListener{ @Override public void onItemClick(String order) { //You can get your value here } } 如下:

adapter

请记住,您应该将活动传递给YourAdapter adapter = new YourAdapter (... , ... , ... , activity.this); ,如下所示:

@InboundChannelAdapter