Counter in RecyclerView updates the TextViews of all rows

1
public class RecyclerViewAdapter_Restaurant extends RecyclerView.Adapter<RecyclerViewAdapter_Restaurant.mealViewHolder> {
private List<Meal> mealList;
private LayoutInflater mInflater;
private Context mContext;
private int cantidadComida=0;




public RecyclerViewAdapter_Restaurant(Context context, List<Meal> mealList) {
    this.mContext = context;
    this.mInflater = LayoutInflater.from(context);
    this.mealList=mealList;

}


@Override
public mealViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = mInflater.inflate(R.layout.row_meals_menu, parent, false);
    mealViewHolder viewHolder = new mealViewHolder(view);


    return viewHolder;
}

@Override
public void onBindViewHolder(final mealViewHolder holder, final int position) {

    holder.tvMealTitulo.setText(mealList.get(position).getName());
    holder.tvMealPrecio.setText(String.valueOf(mealList.get(position).getPrice()));
    holder.tvMealDescripcion.setText(mealList.get(position).getDescription());
    holder.botonMasComida.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

                holder.tvCantidadComida.setText("" + (cantidadComida = cantidadComida + 1));
            //RestaurantDetalle.agregarComida(mealList.get(position).getName()+","+mealList.get(position).getPrice());
        }
    });

    holder.botonMenosComida.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
        if (cantidadComida>0) {
            holder.tvCantidadComida.setText(""+(cantidadComida=cantidadComida-1));
        }
        }
    });

}



@Override
public int getItemCount() {
    return (mealList == null) ? 0 : mealList.size();
}



public static class mealViewHolder extends RecyclerView.ViewHolder {

    CustomFontTextView tvMealTitulo;
    CustomFontTextView tvMealPrecio;
    CustomFontTextView tvMealDescripcion;
    CustomFontTextView tvCantidadComida;

    ImageButton botonMasComida;
    ImageButton botonMenosComida;

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

        tvMealTitulo = (CustomFontTextView) itemView.findViewById(R.id.tvMealTitulo);
        tvMealPrecio = (CustomFontTextView) itemView.findViewById(R.id.tvMealPrecio);
        tvMealDescripcion= ( CustomFontTextView) itemView.findViewById(R.id.tvMealDescripcion);
        tvCantidadComida = (CustomFontTextView) itemView.findViewById(R.id.tvCantidadComida);
        botonMasComida = (ImageButton) itemView.findViewById(R.id.botonMasComida);
        botonMenosComida = (ImageButton) itemView.findViewById(R.id.botonMenosComida);

    }

}

}

By clicking on my button to increase the counter in the Recyclerview, update the TextView of the other rows if I give them Click on their respective button.

    
asked by Eduardo Ricardez 21.04.2017 в 18:11
source

1 answer

1

When clicking you must change the value in the object to be represented in the view that shows the adapter , since the view gets the text of the object, if you add it directly to the view, all the RecyclerView will have this property.

//Agrega propiedad comida.  
holder.tvCantidadComida.setText(mealList.get(position).getCantidadComida());

             holder.botonMasComida.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                    //Almacena el valor en el objeto.         
                   mealList.get(position).setCantidadComida(String.valueOf((cantidadComida = cantidadComida + 1)))
                           // holder.tvCantidadComida.setText("" + (cantidadComida = cantidadComida + 1));
                        //RestaurantDetalle.agregarComida(mealList.get(position).getName()+","+mealList.get(position).getPrice());
                    }
                });

                holder.botonMenosComida.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                    if (cantidadComida>0) {

                      //Almacena el valor en el objeto.  
                   mealList.get(position).setCantidadComida(String.valueOf((cantidadComida=cantidadComida-1)))

                        //holder.tvCantidadComida.setText(""+(cantidadComida=cantidadComida-1));
                    }
                    }
                })

;

    
answered by 21.04.2017 / 18:58
source