-1

I have a RecyclerView with some images,here i want to open different Activitys by clicking on different images... So, i think using the switch statement in the onClick of the adapter will solve my problem but i don't know how to add a switch-if statement in a RecyclerView adapter.i am a beginer in android development so i need some help...

myadapter.java

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ImageViewHolder> {
    @NonNull
    private int[] images;
    public RecyclerAdapter(int[] images){
        this.images =images;
    }
    @Override
    public ImageViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_item2,parent,false);
        ImageViewHolder imageViewHolder = new ImageViewHolder(view);
        return imageViewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ImageViewHolder holder, int position) {
int image_id =images[position];
holder.imagess.setImageResource(image_id);
    }

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

    public static class ImageViewHolder extends  RecyclerView.ViewHolder implements View.OnClickListener {
ImageView imagess;
TextView titless;
        public ImageViewHolder(View itemView) {
            super(itemView);
            imagess = itemView.findViewById(R.id.image);
            titless = itemView.findViewById(R.id.title);
            itemView.setOnClickListener(this);

        }

        @Override
        public void onClick(View v) {
         //   Toast.makeText(itemView.getContext(), "DOWNLOAD ANY TORRENT DOWNLOADER AND OPEN", Toast.LENGTH_LONG).show();

    }
}}

So what i want is : I want to open different activities if the user click the cat image,it should open a activity named cats and if the user clicks the dog image it should open a activity named dogs ...

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Tec Piranha
  • 51
  • 2
  • 10
  • I am not sure why exactly do you want a switch statement here. Simply set click listeners in your `onBindViewHolder()`? – Taseer May 01 '19 at 15:20
  • i want to open different activities if the user click the cat image,it should open a activity named cats and if the user clicks the dog image it should open a activity named dogs – Tec Piranha May 01 '19 at 15:23
  • Added cat activity code. – Taseer May 01 '19 at 16:09

2 Answers2

0
 @Override
    public void onClick(View v) {

        switch(getAdapterPosition()) {

          case 0: 
                  Intent intent = new Intent(context, Cat.class);
                  context.startActivity(intent);
                  break;
          case 1: // Open second activity

      };

}

Returns the Adapter position of the item represented by this ViewHolder.

I have very little experience with Java, I write only in Kotlin. Here is what I have come up with.

Taseer
  • 3,432
  • 3
  • 16
  • 35
  • when clicking case 0 its crashing – Tec Piranha May 01 '19 at 16:14
  • How am I supposed to know your whole code? I only provided with you a 'generic' way of solving the problem. – Taseer May 01 '19 at 16:17
  • and intent would be like , Intent intent = new Intent(itemView.getContext(),cat.class); itemView.getContext().startActivity(intent); – Tec Piranha May 01 '19 at 16:20
  • You need to provide your recycler view adapter with a context from your activity. Include a context parameter in your adapter class constructor. Then you can call `context.startActivity()`. Also read this: https://stackoverflow.com/questions/4186021/how-to-start-new-activity-on-button-click – Taseer May 01 '19 at 16:23
0

Setting the click-events within the Adapter itself is not the best practise.According to the recommended way you should add a callback method and let the Activity \ Fragment to which the Recycler is attached handle after the click events.

How to Proceed,

Step 1: Create an Interface which loosely binds your Adapter to Activity or Fragment.

interface AdapterListener{

void afterAdapterItemClicked(int adapterPosition);

}

This Interface can be created within Adapter itself as an inner member.

Step 2: Let the Activity or Fragment to which the Recycler is attached implement this Interface,So let assume your Activity is named as MenuActivity

class MenuActivity extends Activity implements AdapterListener{


} 

Step 3: Now inside the Activity / Fragment implement the override method

@Override
 void afterAdapterItemClicked(int adapterPosition){

    switch(adapterPosition) {

      case 0: // Move to activity1 
                 break;
      case 1: // Move to activity2 
                 break;

  }

 }

Step 4 : Now calling the method afterAdapterClicked() after the click event

   public static class ImageViewHolder extends  RecyclerView.ViewHolder 
   implements View.OnClickListener {
   ImageView imagess;
   TextView titless;
    public ImageViewHolder(View itemView) {
        super(itemView);
        imagess = itemView.findViewById(R.id.image);
        titless = itemView.findViewById(R.id.title);
        itemView.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {

    mListener.afterAdapterItemClicked(getAdapterPosition());
   }
   }

Step 5: Now to all the curious faces thinking, where in the world mListener landed from, don't worry I saved it for the last.

Now when you create the RecyclerAdapter object(instance) inside your Activity / Fragment you need to pass the current context or this in its constructor.

RecyclerAdapter(arrayOfImages,this);

Now create a new state variable inside your RecyclerAdapter class such as

private AdapterListener mListener;

And then in the constructor of RecyclerAdapter you need to add a variable of type AdapterListener like this and then assign mListener the received value

public RecyclerAdapter(int[] images,AdapterListener mListener){
    this.images = images;
    this.mListener = mListener;
}

And then use mListener inside your inner class ImageViewHolder.

iCantC
  • 2,852
  • 1
  • 19
  • 34
  • thanks for the answer,but its too big,and its not a easy method for begginers,but we can use this in future updates – Tec Piranha May 02 '19 at 13:49