-1

I want to open new activity for every item like for example if user clicks on the first item it should open an activity that shows an image but if user clicks on second item it should show images and texts. Please guide me with this as i am new to Android Studio. Thanks This is my MyAdapter.class code :

    public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
    Context context;
    String data1[],data2[];
    int images[];
    public MyAdapter(Context ct, String s1[], String s2[], int img[]){
        context = ct;
        data1 = s1;
        data2 = s2;
        images = img;
    }

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

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        holder.myText1.setText(data1[position]);
        holder.myText2.setText(data2[position]);
        holder.myImage.setImageResource(images[position]);
    }

    @Override
    public int getItemCount() {
        return data1.length;
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        ImageView myImage;
        TextView myText1,myText2;
        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            myText1 = itemView.findViewById(R.id.myText1);
            myText2 = itemView.findViewById(R.id.myText2);
            myImage = itemView.findViewById(R.id.myImageView);
        }
    }
}

This is my layout screenshot :

This is my layout screenshot

Shay Kin
  • 2,539
  • 3
  • 15
  • 22
Charlie
  • 11
  • 2
  • 1
    What are you actually having an issue with? Starting activities? Intents? Handling the clicks? – Henry Twist Apr 18 '21 at 19:36
  • @HenryTwist I want to open new activity for every item displayed through recycler view. since i am new to Android studio, i am not getting how to do it. – Charlie Apr 18 '21 at 19:52

1 Answers1

0

It looks like, you are not able to distinguish your RecyclerView items. For this an array of integer type (or any type of your choice) can be used to distinguish the RecyclerView items, as shown below:

Create an array int[] type; in the MainActivity, and store the integer values that are used to represent particular Activity.

For example: Lets assume you have 5 items in recycler view, and 3 diffrent activites that you need to call from diffrent recycler view items.

Here,

0 - Represents Activity1

1 - Represents Activity2

2 - Represents Activity3

int[] type = {1,0,2,1,0};

Now pass this array to MyAdapter() constructor, as in the below code:

Context context;
String[] data1;
String[] data2;
int[] images;
int[] type;

public MyAdapter(Context ct, String[] s1, String[] s2, int[] img, int[] type) {
    context = ct;
    data1 = s1;
    data2 = s2;
    images = img;
    this.type = type;
}

Then, inside onBindViewHolder type[] can be used to open diffrent activity from diffrent items:

@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
    holder.myText1.setText(data1[position]);
    holder.myText2.setText(data2[position]);
    holder.myImage.setImageResource(images[position]);

    switch (type[position]) {
        case 0:
            holder.itemView.setOnClickListener(v -> {
                Intent intent = new Intent(context, Activity1.class);
                context.startActivity(intent);
            });
            break;

        case 1:
            holder.itemView.setOnClickListener(v -> {
                Intent intent = new Intent(context, Activity2.class);
                context.startActivity(intent);
            });
            break;

        case 2:
            holder.itemView.setOnClickListener(v -> {
                Intent intent = new Intent(context, Activity3.class);
                context.startActivity(intent);
            });
            break;
    }
}

This my first answer on Stackoverflow. I hope it is clear. If not, please clarify in comments.