rahul
rahul

Reputation: 71

how to send data using bundle inside RecyclerView onclick method

i want to send my name and image in recycler view oclick but i m bit confused should i use paraceble or simple bundle mechanism how to i send and recived it

public class FamousPeople

{
private static final String FAMOUS_NAME = "famous name";
private static final String FAMOUS_PHOTO="photo";
private String name;
private int photo;
private String details;

FamousPeople(String name, int photo) {

    this.name = name;
    this.photo = photo;
}
public  FamousPeople(){


}


public String getDetails() {
    return details;
}

public void setDetails(String details) {
    this.details = details;
}

public int getPhoto() {
    return photo;
}

public void setPhoto(int photo) {
    this.photo = photo;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}


   }

BiographyViewHolder class in which i have to pass my images and text data

 class BiographyViewHolder extends RecyclerView.ViewHolder
       implements View.OnClickListener {

   public TextView textView;
  public ImageView imageView;
List<FamousPeople> famousPeoples=GetData.getListdata();


public BiographyViewHolder(View itemView)
{
    super(itemView);
    itemView.setOnClickListener(this);
    textView= (TextView) itemView.findViewById(R.id.country_name);
    imageView= (ImageView) itemView.findViewById(R.id.country_photo);


}

@Override
public void onClick(View view) {
    Intent  intent=new Intent();
    Bundle bundle=new Bundle();

    intent.putExtras(bundle);
    view.getContext().startActivity(intent);

}

 }

GetData class getting data from this class

     public  class GetData {
    public static List<FamousPeople> getListdata() {
    List<FamousPeople> famousPeoples = new ArrayList<>();
    famousPeoples.add(new FamousPeople("rahul", R.drawable.amitabh));
    famousPeoples.add(new FamousPeople("sumit", R.drawable.cvraman));
    famousPeoples.add(new FamousPeople("neha", R.drawable.indira));
    famousPeoples.add(new FamousPeople("surbhi", R.drawable.kishorkumar));
    famousPeoples.add(new FamousPeople("rahul", R.drawable.modi));
    famousPeoples.add(new FamousPeople("sumit", R.drawable.mother));
    famousPeoples.add(new FamousPeople("neha", R.drawable.nehru)) 

    return famousPeoples;
}

Upvotes: 0

Views: 5017

Answers (3)

Amit Vaghela
Amit Vaghela

Reputation: 22945

To get image and name , you have to do something like this,

For ex : YOUR CLICK EVENT to send data onClick();

ViewHolder VHheaderItem = (ViewHolder) view;

    VHheaderItem.image_detail.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Intent i = new Intent(CurrentClass.this, AnotherClass.class);
                            i.putExtra("Name", listItems1.get(position).getVarName(););
                            startActivity(i);
                        }
                    });

To receive it ,

String drName = getIntent().getStringExtra("Name");

Now if you want to pass image through intent then

imageView.buildDrawingCache();
Bitmap image= imageView.getDrawingCache();

Bundle extras = new Bundle();
extras.putParcelable("imagebitmap", image);
intent.putExtras(extras);
startActivity(intent);

To receive it,

Bundle extras = getIntent().getExtras();
Bitmap bmp = (Bitmap) extras.getParcelable("imagebitmap");

image.setImageBitmap(bmp);

Go through get ImageView's image and send it to an activity with Intent for more detail and how to pass images through intent

Upvotes: 0

rahul
rahul

Reputation: 71

@Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view=inflater.inflate(R.layout.fragment_detail, container, false);
   textView= (TextView) view.findViewById(R.id.bioname);
    imageView= (ImageView) view.findViewById(R.id.imageView);
    Bundle getBundle = null;
    getBundle = getActivity().getIntent().getExtras();

    String name = getBundle.getString("NAME");
    int id = getBundle.getInt("IMAGE");
    textView.setText(name);
    imageView.setImageResource(id);
    return view;
}

Upvotes: 0

ThaiPD
ThaiPD

Reputation: 3741

try to edit your onClick() function as below. All you need is get data from your list, pointed by getAdapterPosition and then pass them to bundle

    @Override
        public void onClick(View v) {
            Intent  intent=new Intent();
            Bundle bundle=new Bundle();
            bundle.putInt("IMAGE", famousPeoples.get(getAdapterPosition()).getPhoto());
            bundle.putString("NAME", famousPeoples.get(getAdapterPosition()).getName());
intent.putExtras(bundle);
    view.getContext().startActivity(intent);

}

Upvotes: 2

Related Questions