Hi, I am new to Android, and I have not yet been able to solve a problem I have. Code a recyclrerViewAdapter that allows me to display a list of products on the screen. I want to emphasize that this list of items has the options to add products or remove them.
The code is as follows:
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewHolder> {
Context context;
ArrayList<Productos> items;
String cantidadProductos;
public RecyclerViewAdapter(Context context, ArrayList<Productos> items) {
this.context = context;
this.items = items;
}
@NonNull
@Override
public RecyclerViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.item, parent, false);
RecyclerViewHolder viewHolder = new RecyclerViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(@NonNull RecyclerViewHolder holder, int position) {
holder.img_item.setImageResource(items.get(position).getImagen());
holder.cardView.setOnClickListener(onClickListener);
holder.cardView.setTag(holder);
holder.setOnclickListener();
}
View.OnClickListener onClickListener = (new View.OnClickListener() {
@Override
public void onClick(View view) {
RecyclerViewHolder sh = (RecyclerViewHolder) view.getTag();
int posicion = sh.getAdapterPosition();
cantidadProductos = Integer.toString(sh.cont);
Intent intent = new Intent(context, DialogoActivity.class);
intent.putExtra("imagen", items.get(posicion).getImagen());
intent.putExtra("nombre", items.get(posicion).getNombre());
intent.putExtra("precio", items.get(posicion).getPrecio());
intent.putExtra("cantidadProductos", cantidadProductos);
context.startActivity(intent);
}
});
@Override
public int getItemCount() {
return items.size();
}
}
and on the screen you see something like this:
This would be the RecyclerViewHolder where you implement the methods that allow you to add or decrease the products:
public class RecyclerViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
ImageView img_item;
ImageButton btn_add, btn_remove;
TextView cantidad_producto;
CardView cardView;
int cont = 0;
public RecyclerViewHolder(View itemView) {
super(itemView);
img_item = (ImageView) itemView.findViewById(R.id.img_items);
btn_add = (ImageButton) itemView.findViewById(R.id.btn_agregar);
btn_remove = (ImageButton) itemView.findViewById(R.id.btn_remover);
cantidad_producto = (TextView) itemView.findViewById(R.id.cantidad_agregada);
cardView = (CardView) itemView.findViewById(R.id.onClickItems);
}
public void setOnclickListener(){
btn_remove.setOnClickListener(this);
btn_add.setOnClickListener(this);
}
@Override
public void onClick(View view) {
if (view.getId() == R.id.btn_agregar){
cont++;
}else if (view.getId() == R.id.btn_remover && cont > 0){
cont--;
}else if (view.getId() == R.id.btn_remover && cont == 0){
Toast.makeText(view.getContext(),"No puede eliminar los productos", Toast.LENGTH_SHORT).show();
}
cantidad_producto.setText(Integer.toString(cont));
}
}
by clicking on one of the items of products shown in the previous image, an activity that works as a dialogue is opened, and it receives and shows the user more detail of the products:
As you can see, this dialog allows you to modify also the amount of products that can be added. We already manage to send the quantity data from the item to the dialog, but I do not know how to resend this data from the activity to the items that are in the recyclerVewAdapter.
this would be the activity code:
public class DialogoActivity extends Activity implements View.OnClickListener {
ImageView imagen;
TextView nombre, precio, cantidadProductos;
ImageButton btn_remove, btn_add;
int cont;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dialogo);
imagen = (ImageView) findViewById(R.id.img_dialog);
nombre = (TextView) findViewById(R.id.titulo_dialog);
precio = (TextView) findViewById(R.id.precio_dialog);
cantidadProductos = (TextView) findViewById(R.id.text_cantidad);
btn_add = (ImageButton) findViewById(R.id.btn_dialog_agregar);
btn_remove = (ImageButton) findViewById(R.id.btn_dialog_remover);
Intent intent = getIntent();
int imagenProducto = intent.getExtras().getInt("imagen");
String nombreProducto = intent.getExtras().getString("nombre");
String precioProducto = intent.getExtras().getString("precio");
String cantidad = intent.getExtras().getString("cantidadProductos");
cont = Integer.parseInt(cantidad);
imagen.setImageResource(imagenProducto);
nombre.setText(nombreProducto);
precio.setText(precioProducto);
cantidadProductos.setText(cantidad);
setOnClickListener();
}
public void setOnClickListener(){
btn_remove.setOnClickListener(this);
btn_add.setOnClickListener(this);
}
@Override
public void onClick(View view) {
if (view.getId() == R.id.btn_dialog_agregar){
cont++;
}else if (view.getId() == R.id.btn_dialog_remover && cont > 0){
cont--;
}else if (view.getId() == R.id.btn_dialog_remover && cont == 0){
Toast.makeText(view.getContext(),"No puede eliminar los productos", Toast.LENGTH_SHORT).show();
}
cantidadProductos.setText(Integer.toString(cont));
}
}
I appreciate the help you offer me ...