Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Start activity from ViewHolder onclick

Tags:

android

I´m tryng to start a new activity when the user clicks on a RecycleView element . The problem is that only one field of object I want to pass to JSON is show in the row_item Layout and don´t know how to retrieve the other 3 object fields.

Here are the steps I´m following .

First I send a list of objects (Poliza) to the adapter . Second I show only one field of the object (poliza) in the RecycleView . Third I want to start a new activity when the user clicks on the RecycleView item , sending a JSON of the object with the other object fields that are not show in the row_item .

Here is my adapter

public class PolizasAdapter extends RecyclerView.Adapter<PolizasAdapter.PolizaViewHolder> {

    private List<Poliza> listapoliza = null;
    private Context context;

    public PolizasAdapter(  List<Poliza> listapoliza) {
        this.listapoliza = listapoliza;
    }

    public class PolizaViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        // each data item is just a string in this case
        TextView txtPoliza;

        public PolizaViewHolder(View view) {
            super(view);
            //Agregamos un onclickListener
            view.setOnClickListener(this);
            context = view.getContext();
            //Hacemos referencia a las vistas del rowitem
            txtPoliza = (TextView) view.findViewById(R.id.txt_poliza);
        }

        @Override
        public void onClick(View view) {
            //Evento que que se genera cuando se da click
            TextView texto = (TextView) view.findViewById(R.id.txt_poliza);
            String str = texto.getText().toString();
            System.out.println(str);
            //Iniciamos actividad mostrando el detalle

             Intent intent = new Intent(context , DetalleActivity.class);
             intent.putExtra("id_poliza",str);
            //Whats next ?
            //How to add the other 3 fields of my object to the putExtra


        }
    }

    @Override
    public PolizasAdapter.PolizaViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //Creamos una nueva vista
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_item, parent, false);
        PolizaViewHolder viewHolder = new PolizaViewHolder(view);
        return viewHolder;
    }

     @Override
    public void onBindViewHolder(PolizasAdapter.PolizaViewHolder holder, int position) {

        holder.txtPoliza.setText("Poliza : " + String.valueOf(listapoliza.get(position).getPoliza()));
    }

   )
    @Override
    public int getItemCount() {
        return listapoliza.size();
    }
}
like image 481
jcromanu Avatar asked Apr 18 '16 00:04

jcromanu


People also ask

How to handle click event on RecyclerView?

Update the ViewHolder to take in onClick() as a parameter. In the initializer, call setOnClickListener{} on the itemView . That's it! Your RecyclerView is now responsive so time to get your click on!

How to add click listener on button in RecyclerView Android?

In your ViewHolder class in your RecyclerView adapter, implement View. OnClickListener, bind the listener to the view. In the onClick method, call the onClick method of the interface OnItemClickListener. This should be passed in from your RecycyclerView's constructor.


1 Answers

For starting the Activity you should do context.startActivity(intent); As for the 3 JSON parameter you're talking about, i couldn't find any reference of them in your code (there's no reference to any JSON whatsoever). I guess you might be talking about a field from Poliza object, but i can't be sure.

like image 154
Jonatan Collard Bovy Avatar answered Sep 23 '22 20:09

Jonatan Collard Bovy