1

Within my Recycler Adapter, I have the following:

public void onBindViewHolder(ViewHolder holder, int position) {
    User userItem = mDataset.get(position);

    holder.itemView.setTag(userItem);

    ...
}

Within my layout, I have a clickable element:

...

<FrameLayout
    android:id="@+id/icon"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:background="@color/white"
    android:layout_marginRight="15dp"
    android:layout_marginBottom="15dp"
    android:onClick="showActivity">

...

On click, it should show the next Activity. I need to pass the tag that I attached to it so that I can use the data in the next Activity.

Here is showActivity within my activity:

public void showActivity(View view) {
    Intent intent = new Intent(this, SecondActivity.class);

    // pass the model to the next activity

    startActivity(intent);
}

How can I do this?

4 Answers4

0

When you need to pass data from one activity to another, you use Bundle. Unfortunately you can't pass any object you want, you need convert it to Bundle.

Also, where is this onBindViewHolder() method? If it is in Activity, just save the data in a class variable and use it on showActivity(). If it is in adapter, you can have a public method to achieve an item's tag and use that method in the activity.

NecipAllef
  • 486
  • 1
  • 5
  • 18
0

Your question is not clear but i think you want to pass selected item to next activity but instead passing its model just pass its data by bundle in click listener.

Try this:

public class AdapterClass extends RecyclerView.Adapter<AdapterClass.MyViewHolder> {
    private LayoutInflater inflater;
    private Context context;
List<Information>data= Collections.emptyList();
    public AdapterClass(Context context,List<Information>data){
        this.context=context;

        inflater= LayoutInflater.from(context);
        this.data=data;
    }
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
       View view= inflater.inflate(R.layout.custom_row,parent,false);
        MyViewHolder holder=new MyViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        Information current=data.get(position);
        holder.title.setText(current.title);
        holder.icon.setImageResource(current.iconId);

    }

    @Override
    public int getItemCount() {
        return data.size();
    }
    class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
        private final Context context;
        TextView title;
        ImageView icon;

        public MyViewHolder(View itemView) {
            super(itemView);

            context = itemView.getContext();

            title=(TextView)itemView.findViewById(R.id.listText);
           icon=(ImageView)itemView.findViewById(R.id.listIcon);

            //Set click listener on list item
            itemView.setClickable(true);
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
             //Get data of clicked item    
             Information item = data.get(getPosition());

             Intent intent = new Intent(context, NextActivity.class);

             //Create bundle which include title and icon and pass it to nextactivity
             Bundle bundle = new Bundle();
             bundle.putString("KEY_LIST_ITEM_TITLE", item.title);
             bundle.putInt("KEY_LIST_ITEM_ICON", item.iconId);
             //set bundle to intent
             intent.putExtras(bundle);
             context.startActivity(intent);
        }
    };
}

Now in NextActivity simply get title and icon from bundle

  onCreate() {
      .....
      String title = getIntent().getExtras().getString("KEY_LIST_ITEM_TITLE");
      int icon = getIntent().getExtras().getString("KEY_LIST_ITEM_ICON");
  }

Note: if you want to pass object then use bundle.putParceable("ANY_KEY",object) and object class need to implement Parceable interface.

Sabeeh
  • 1,478
  • 15
  • 15
0

Your model should implement Parcelable,like that:

public class MyParcelable implements Parcelable {
 private int mData;

 public int describeContents() {
     return 0;
 }

 public void writeToParcel(Parcel out, int flags) {
     out.writeInt(mData);
 }

 public static final Parcelable.Creator<MyParcelable> CREATOR
         = new Parcelable.Creator<MyParcelable>() {
     public MyParcelable createFromParcel(Parcel in) {
         return new MyParcelable(in);
     }

     public MyParcelable[] newArray(int size) {
         return new MyParcelable[size];
     }
 };

 private MyParcelable(Parcel in) {
     mData = in.readInt();
 }

}

Then use that (intent.putExtra("mymodel",model);)pass your model to the second Activity.

Andy.Zhao
  • 250
  • 3
  • 16
0

Whenever you want to pass the data from one activity to other activity , you can pass it using bundle and intent. As you are using intent to start second activity,you can set data and in second activity you can get the same data using set like this :-

Intent mIntent = new Intent(this, SecondActivity.class);
Bundle extras = mIntent.getExtras();
extras.putString(key, value);  

or

Intent mIntent = new Intent(this, SecondActivity.class);
Bundle mBundle = new Bundle();
mBundle.putString(key, value);
mIntent.putExtras(mBundle);

or Intent mIntent = new Intent(this, SecondActivity.class); mIntent.putExtra(key, value);

and get those values like this:-

String value = getIntent().getExtras().getString(key) ;