I'm developping a market place app for some products. In one of the activities of the app i have a CardView
where the products are shown with their info, such as brand, name, tipe of product, etc. For that i made an object called Product
.
Now, when a CardView
is clicked, another activity is launched where it shows the same info described before and more other details.
So far i made the card view load the name tipe brand etc. for each individual item on the CardView
and launch the activity where all the product info is displayed.
I need to pass the info of the Item clicked on the CardView
to another activity where i can show all the other information of the product.
I'm ussing Room libraries.
A friend recommended me to use Live data. Thanks!
Here somo code:
Product adapter:
public class ProductoAdapter extends RecyclerView.Adapter<ProductoAdapter.ProductoViewHolder> {
private final ArrayList<Producto> producto;
private final OnCustomClickListener listener;
public ProductoAdapter( ArrayList<Producto> producto, OnCustomClickListener listener) {
this.producto = producto;
this.listener = listener;
}
//el activty que corresponde a cada layout de los item de la lista
@NotNull
@Override
public ProductoViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.cv_producto, parent, false);
return new ProductoViewHolder(view);
}
@Override
public void onBindViewHolder(ProductoViewHolder holder, int position) {
holder.bind(producto.get(position),listener);
}
@Override
public int getItemCount() { return producto.size(); }
public static class ProductoViewHolder extends RecyclerView.ViewHolder{
private final View mItemView;
public ProductoViewHolder(View itemView) {
super(itemView);
this.mItemView = itemView;
}
private void bind (Producto producto, OnCustomClickListener listener){
ImageView ivFoto = itemView.findViewById(R.id.iv_foto);
TextView tvNombre = itemView.findViewById(R.id.tv_nombre);
TextView tvPrecio = itemView.findViewById(R.id.tv_precio);
TextView tvDetalles = itemView.findViewById(R.id.tv_detalles);
CardView cardView = itemView.findViewById(R.id.cv_productos);
tvNombre.setText(producto.getNombre());
tvPrecio.setText(String.valueOf(producto.getPrecio()));
tvDetalles.setText(producto.getDetalles());
Picasso.with(mItemView.getContext()).load(producto.getFoto()).into(ivFoto);
cardView.setOnClickListener(v -> {
listener.onItemClick(producto,getAdapterPosition());
});
}
}
}
Product activity:
public class GeneralActivity1 extends AppCompatActivity implements OnCustomClickListener {
private ArrayList<Producto> producto = new ArrayList<>();
private ArrayList<Producto> complementarios = new ArrayList<>();
private ImageButton ivMenuProdex;
private NavigationView nvProd;
private DrawerLayout dlProd ;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_general1);
nvProd = findViewById(R.id.nav_view_prod);
dlProd = findViewById(R.id.dl_prod);
ivMenuProdex = findViewById(R.id.iv_menu_prodex);
getProductos();
}
private void getProductos (){
producto.add(new Producto("Cuchilla Suiza","Cuchilla",
"Eskisltuna","Encabado en asta de ciervo. Detalles del cavo en bronce y alpaca.",25,6000,"https://scontent.faep8-2.fna.fbcdn.net/v/t1.0-9/136767977_3421206694658189_1630175753886517576_o.jpg?_nc_cat=108&ccb=2&_nc_sid=730e14&_nc_eui2=AeECbP_Ezm1xWnfoljYhbo81WeUbkftb86VZ5RuR-1vzpczPH0tRhrCIV_0LmsU5BDI&_nc_ohc=VqPdhfdgapEAX-QUJq0&_nc_ht=scontent.faep8-2.fna&oh=709493376ea724b54952cfc11f13611b&oe=602DF234"));
@Override
public void onItemClick (Producto producto, int position) {
Intent ProductoClickeado = new Intent(this, ExpandidoActivity2.class);
startActivity(ProductoClickeado);
}
private void rvproductos(){
LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
RecyclerView rvProductos = findViewById(R.id.rv_productos);
rvProductos.setLayoutManager(layoutManager);
ProductoAdapter adapter1 = new ProductoAdapter(producto,this);
rvProductos.setAdapter(adapter1);
}