How to start new activity from recyclerview?

64
February 12, 2019, at 00:10 AM

I want to start new activity and also passing JSON data to the new activity when i click an item on the recyclerview. i followed a video in youtube: https://www.youtube.com/watch?v=OfsiccfUWVc&index=6&list=PLaoF-xhnnrRW_FGeacuT1VLqnRMKfpp4v but the video didn't show how to go to new activity once i clicked the item on recyclerview. from the video, i wanted to click on the item and then new activity will bring me to the 'more detail page' about the item.

i have tried to do the intent but i am not sure how to call from the new main activity

this is my adapter code:

public class LonghouseAdapter extends RecyclerView.Adapter<LonghouseViewHolder> {
Context context;
List<LonghousesCategoryOne> longhousesCategoryOneList;
List<Category> categories;
private OnItemClickListener mlistener;

public LonghouseAdapter(Context context, List<LonghousesCategoryOne> longhousesCategoryOneList) {
    this.context = context;
    this.longhousesCategoryOneList = longhousesCategoryOneList;
}
@NonNull
@Override
public LonghouseViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View itemView = LayoutInflater.from(context).inflate(R.layout.longhouse_item_layout,null);
    return new LonghouseViewHolder(itemView);
}
@Override
public void onBindViewHolder(@NonNull LonghouseViewHolder holder, final int position) {
    holder.txt_price.setText(new StringBuilder("").append(longhousesCategoryOneList.get(position).Price));
    holder.txt_longhouse_name.setText(new StringBuilder("").append(longhousesCategoryOneList.get(position).Name));
    Picasso.with(context)
            .load(longhousesCategoryOneList.get(position).Link)
            .into(holder.img_longhouse);
    holder.setItemClickListener(new IItemClickListener() {
        @Override
        public void onClick(View v) {
          // Common.currentCategory = categories.get(position);
           //context.startActivity(new Intent(context, descriptionsActivity.class));
        }
    });

}
@Override
public int getItemCount() {
    return longhousesCategoryOneList.size();
}
}

this is my viewholder code:

public class LonghouseViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
ImageView img_longhouse;
TextView txt_longhouse_name, txt_price;

public void setItemClickListener(IItemClickListener itemClickListener) {
    this.itemClickListener = itemClickListener;
}
IItemClickListener itemClickListener;

public LonghouseViewHolder(View itemView) {
    super(itemView);
    img_longhouse = (ImageView) itemView.findViewById(R.id.image_longhouse);
    txt_longhouse_name = (TextView) itemView.findViewById(R.id.txt_longhouse_name);
    txt_price = (TextView) itemView.findViewById(R.id.txt_longhouse_price);
    itemView.setOnClickListener(this);

}
@Override
public void onClick(View v) {
    itemClickListener.onClick(v);
}
}

Can anyone guide me on this? Thank you

Answer 1

As @rgaraisayev recommends in your onClick() listener uncomment the call to startActivity() i.e.

holder.setItemClickListener(new IItemClickListener() {
    @Override
    public void onClick(View v) {
        Common.currentCategory = categories.get(position);
        context.startActivity(new Intent(context, descriptionsActivity.class));
    }
});
READ ALSO
Drawing rectangles that fill up with gradient depending on the value

Drawing rectangles that fill up with gradient depending on the value

I want to have three separate rectangles that would fill up with gradientDepending on the value X the gradient would be placed differently

62
Android Studio an generates error when creating an apk

Android Studio an generates error when creating an apk

I got the error specified below when I tried to generate the apk of my app in Android Studio ,please help

51
Different screen size issues - how to fix that?

Different screen size issues - how to fix that?

I have some fixed height and width, and margins in my layoutI think it is causing issue when I try to support multiple screen sizes

61
Please configure Android SDK Android Studio

Please configure Android SDK Android Studio

I am working with flutter and have the message Please configure Android SDK, I was looking at other answers but they don't fit, I click on configure and get the following imageYou can see its asking me for locations, I checked and they are correct

46