1

I succeeded to put onClickListener for the recyclerView,but my problem here is

Every item in the recyclerview go to the same new activity!!!

Could we solve it with item position or what??

I'm trying to send each RecyclerView (ITEM or cell) to a new activity. Say the first one is Food, send it to a food page, the next is health, send it to a health page... etc. I'm trying to change the activity.class it sends the user to for each different view from the activity main.

My Adapter class

import java.util.List;

    public class Adapter extends RecyclerView.Adapter<Adapter.myViewHolder>{



        Context mcontext;
        List<item> mData;
        RecyclerView recyclerView;







        public Adapter(Context mcontext, List<item> mData) {
            this.mcontext = mcontext;
            this.mData = mData;
        }

        @NonNull
        @Override
        public myViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

            LayoutInflater inflater=LayoutInflater.from(mcontext);
            View v =inflater.inflate(R.layout.card_item,parent,false);

            return new myViewHolder(v);
        }

        @Override
        public void onBindViewHolder(@NonNull final myViewHolder holder, final int position) {

            holder.background_img.setImageResource(mData.get(position).background);
            holder.tv_title.setText(mData.get(position).profilename);
            holder.profile_photo.setImageResource(mData.get(position).profilephoto);
            holder.tv_subtitle.setText(mData.get(position).subtitle);
            holder.share.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
                    sharingIntent.setType("text/plain");
                    String shareBody = "Your body here";
                    String shareSub = "Your subject here";
                    sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, shareSub);
                    sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
                    mcontext.startActivity(Intent.createChooser(sharingIntent, "Share using"));
                }
            });

            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {




                    Intent intent = new Intent(mcontext, SomaBay_bulding_Structure.class);

                    mcontext.startActivity(intent);


                }
            });

        }



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

        public class myViewHolder extends RecyclerView.ViewHolder{

            ImageView profile_photo,background_img;
            TextView tv_title,tv_subtitle;
            Button share;


            public myViewHolder(View itemView) {
                super(itemView);
                profile_photo=itemView.findViewById(R.id.profile_img);
                background_img=itemView.findViewById(R.id.card_background);
                tv_title=itemView.findViewById(R.id.card_title);
                tv_subtitle=itemView.findViewById(R.id.card_subtitle);
                 share = itemView.findViewById(R.id.button_share);


            }


        }
    }
Android
  • 1,420
  • 4
  • 13
  • 23
Ezzo
  • 15
  • 6

1 Answers1

0

As I understood your question: you need to start activities depends on different item type, if you have this param:

            Intent intent;
            if(mData.get(position).type.equals("FOOD")){
              intent =  new Intent(mcontext, FoodActivity.class);
            }else if(mData.get(position).type.equals("HEALTH")){
                intent = new Intent(mcontext, HealthActivity.class);
            }else if(...){
                //
            }
            startActivity(intent);
Anton Sarmatin
  • 505
  • 2
  • 8