rahul vyas rahul vyas - 5 months ago 60
Android Question

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;
}

Answer

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);

}
Comments