RecyclerView Click to pass data to new activity

回眸只為那壹抹淺笑 提交于 2019-12-02 07:36:30

You can pass the data set into the adapter constructor and reference the data set whenever an item is clicked. Most of the trick is inside the ViewHolder implementation. Assuming that the data set contains String values, it should look more or less like this:

public class ViewHolder extends RecyclerView.ViewHolder {
  public ViewHolder(LayoutInflater inflater, ViewGroup parent) {
    super(inflater.inflate(R.layout.singleitemview, parent, false));
    itemView.setOnClickListener(new View.OnClickListener() {

      @Override
      public void onClick(View v) {
        int position = getAdapterPosition();

        Context context = v.getContext();
        Intent intent = new Intent(context, SingleItemView.class);
        intent.putExtra("com.package.sample.ITEM_DATA", mDataset.get(position));
        context.startActivity(intent);
      }
    });
  }
}

pass in intent extras.there are several methods available like putString, putLong, putXXX etc. if it is your custom class then implement serialize interface and then put data.and in respective activity get it by using getIntent.getExtras("pass same key that you use when putting data into extras"). and you can get your desired data.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!