Event in a button of each Item of a Firebase Recycler Adapter

0

I have a RecyclerView loaded from Firebase data, I need to add a functionality to a button that is in each item, which is to add a product to the shopping cart.

The problem that I have happens when there is more than one item in the recycler, because when you click on the button, an item is added correctly, but the others can not be added anymore.

What I want is to make the onclik for each item and apparently only take it once.

@Override
        protected void populateViewHolder(final FriendsViewHolder viewHolder, final Menu model, int position) {

            final String item_id  = getRef(position).getKey();

            final String rest_id = getIntent().getStringExtra("rest_id");


            viewHolder.setName(model.getNombre());
            viewHolder.setDescrpcion(model.getDescripcion());
            viewHolder.setImage(getApplicationContext(),model.getThumb_image());
            viewHolder.agregaBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mEstado_pedidos.equals("sin_pedidos")){

                        Map pedidoMap = new HashMap();
                        pedidoMap.put("Solicitud_Pedido/"+mCurrent_user.getUid() + "/" + rest_id + "/" + item_id + "/tipo_pedido", "solicitado");
                        pedidoMap.put("Solicitud_Pedido/"+rest_id + "/" + mCurrent_user.getUid() + "/" + item_id +"/tipo_pedido", "recibido");

                        mRootRef.updateChildren(pedidoMap, new DatabaseReference.CompletionListener() {
                            @Override
                            public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {

                                if (databaseError != null){

                                    Toast.makeText(getApplicationContext(), "Hubo un error al enviar la solicitud" , Toast.LENGTH_SHORT).show();
                                }
                                FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);

                                fab.setEnabled(true);
                                fab.setVisibility(View.VISIBLE);

                                mEstado_pedidos = "con_pedido";
                                Button agregaBtn = (Button)findViewById(R.id.btn_agregar);
                                agregaBtn.setText("Eliminar del carrito");

                            }
                        });

                    }
                }
            });

            final String list_user_id  = getRef(position).getKey();

            viewHolder.mView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                }
            });


        }

this is my class ViewHolder :

public static class FriendsViewHolder extends RecyclerView.ViewHolder{
    View mView;Button agregaBtn;


    public FriendsViewHolder(View itemView) {
        super(itemView);

        mView = itemView;
        agregaBtn = (Button)mView.findViewById(R.id.btn_agregar);


    }


    public void setName(String nombre){

        TextView mName = (TextView) mView.findViewById(R.id.nombre_item);
        mName.setText(nombre);
    }

    public void setDescrpcion(String descrpcion){

        TextView mDescripcion = (TextView) mView.findViewById(R.id.descripcion_item);
        mDescripcion.setText(descrpcion);
    }

    public void setImage(final Context context, String thumb_image){

        ImageView mImagen = (ImageView) mView.findViewById(R.id.imagen_item);
        Picasso.with(context).load(thumb_image).into(mImagen);

    }

}
    
asked by Juan Solano 23.07.2017 в 22:08
source

0 answers