Open a new Activity by clicking on a Cardview

1

I have a Recyclerview and a Cardview the problem is that I want to open a new activity by selecting from the 6 different items that the list shows, I have obtained the position of the items when I press but when I try to implement a intent does not work for me from now thanks Error when placing Intent intent = new Intent(RecyclerAdapter.this, ActividadPrincipal.class); startActivity(intent); shows me the error that the method can not solve it and shows startActivity(intent); in red

    public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {

    private String[] titles = {"Asistente",
            "Expositor",
            "Administrador",
            "Galeria",
            "Ubicacion",
            "Guia rapida"};

    private String[] details = {"",
            "Item two details", "Item three details",
            "Item four details", "Item file details",
            "Item six details", "Item seven details",
            "Item eight details"};

    private int[] images = { R.drawable.defecto,
            R.drawable.defecto,
            R.drawable.defecto,
            R.drawable.defecto,
            R.drawable.defecto,
            R.drawable.defecto,
            R.drawable.defecto,
            R.drawable.defecto };

    class ViewHolder extends RecyclerView.ViewHolder{

        public int currentItem;
        public ImageView itemImage;
        public TextView itemTitle;
        public TextView itemDetail;

        public ViewHolder(View itemView) {
            super(itemView);
            itemImage = (ImageView)itemView.findViewById(R.id.item_image);
            itemTitle = (TextView)itemView.findViewById(R.id.item_title);
            itemDetail = (TextView)itemView.findViewById(R.id.item_detail);

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override public void onClick(View v) {
                    int position = getAdapterPosition();

                    Snackbar.make(v, "Click detected on item " + position,
                            Snackbar.LENGTH_LONG)
                            .setAction("Action", null).show();

                }
            });
        }
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View v = LayoutInflater.from(viewGroup.getContext())
                .inflate(R.layout.card_layout, viewGroup, false);
        ViewHolder viewHolder = new ViewHolder(v);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, int i) {
        viewHolder.itemTitle.setText(titles[i]);
        viewHolder.itemDetail.setText(details[i]);
        viewHolder.itemImage.setImageResource(images[i]);
    }

    @Override
    public int getItemCount() {
        return titles.length;
    }
}
    
asked by Ashley G. 21.12.2016 в 20:03
source

3 answers

3

Declare the following in your constructor

 public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {

 private Context context;

 public RecyclerAdapter(Context context) {
      this.context = context;     
 } 

Inside your onClick

   itemView.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
           Intent intent= new Intent(context, MiNuevoActivity.class);
           context.startActivity(intent);
      }
  });

In the activity where you are creating the instance of your RecyclerAdapter you pass the context, example:

 //this es el contexto de tu Actividad
 new RecyclerAdapter(this); 

Greetings.

    
answered by 21.12.2016 / 20:11
source
2

You can do it within onBindViewHolder() you add a View.OnClickListener , make sure you have the context to start the Actitivity ( startActivity() ), modify the constructor to receive it:

private Context context;

     public RecyclerAdapter(Context context) {
          this.context = context;     
     } 

this would be how it would be done according to your code:

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    viewHolder.cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

             Intent intent = new Intent(context, OtraActivity.class); 
             context.startActivity(intent);

            }
        });
    }

    viewHolder.itemTitle.setText(titles[i]);
    viewHolder.itemDetail.setText(details[i]);
    viewHolder.itemImage.setImageResource(images[i]);

}
    
answered by 21.12.2016 в 20:18
0

The adapter has to call a method that is in the fragment (or activity), to perform this action.

But first you must declare it. Example

1) add this to the adapter:

private NombreFragment nombreFragment; 

2) and call the fragment method (where you detect the click) like this:

nombreFragment.lanzaActividad(/*algunParametroSiDeseas*/); 

3) And in the fragment declare:

public void lanzaActividad(/*String algunParametroSiDeseas*/){
    Intent intento = new Intent(context, CheckNuevo.class);
    /*i.putExtra("parametro", "algunParametroSiDeseas");*/
    startActivity(intento );
}
    
answered by 21.12.2016 в 20:15