I'm want to do a checkbox on each cart.
But when I run my code, the checkbox was not working perfectly, seem like have a problem with the position state.
When I check or uncheck my checkbox, the other checkboxes also follow to check
customer_cart_view_fragment.java
public void onStart(){
super.onStart();
adapter.startListening();
}
@Override
public void onStop(){
super.onStop();
adapter.stopListening();
}
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState){
View v = inflater.inflate(R.layout.fragment_customer_cart_view_fragment,container,false);
cartList = (RecyclerView)v.findViewById(R.id.cartProductRecyclerView);
cartList.setHasFixedSize(true);
cartList.setLayoutManager(new LinearLayoutManager(getActivity()));
arrayList = new ArrayList<Cart>();
final String user=auth.getInstance().getCurrentUser().getUid();
databaseReference = FirebaseDatabase.getInstance().getReference().child("Account").child(user).child("cart");
options = new FirebaseRecyclerOptions.Builder<Cart>().setQuery(databaseReference,Cart.class).build();
adapter = new FirebaseRecyclerAdapter<Cart, FirebaseCartProductViewHolder>(options){
@Override
protected void onBindViewHolder(@NonNull final FirebaseCartProductViewHolder firebaseCartProductViewHolder,int i,@NonNull final Cart cart){
firebaseCartProductViewHolder.itemId.setText(cart.getItemID());
firebaseCartProductViewHolder.productName.setText(cart.getProductName());
firebaseCartProductViewHolder.productPrice.setText(cart.getPrice());
Picasso.with(getActivity()).load(cart.getImageURL()).into(firebaseCartProductViewHolder.productImage);
databaseReference = FirebaseDatabase.getInstance().getReference().child("Account").child(user).child("cart").child(firebaseCartProductViewHolder.itemId.getText().toString());
databaseReference.keepSynced(true);
databaseReference.addValueEventListener(new ValueEventListener(){
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot){
if(dataSnapshot != null && dataSnapshot.exists()){
String status = dataSnapshot.child("status").getValue().toString();
String str1 = "check";
if(status.equals(str1))
{
firebaseCartProductViewHolder.check1.setChecked(true);
}
else
{
firebaseCartProductViewHolder.check1.setChecked(false);
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError){
}
});
firebaseCartProductViewHolder.check1.setOnCheckedChangeListener(null);
firebaseCartProductViewHolder.check1.isChecked();
firebaseCartProductViewHolder.check1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
@Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked){
DatabaseReference dbref;
dbref = FirebaseDatabase.getInstance().getReference().child("Account").child(user).child("cart").child(firebaseCartProductViewHolder.itemId.getText().toString());
System.out.println(firebaseCartProductViewHolder.itemId.getText().toString());
if(firebaseCartProductViewHolder.check1.isChecked()) {
dbref.child("status").setValue("check");
}
else
{
dbref.child("status").setValue("pending check");
}
}
});
/*firebaseCartProductViewHolder.check1.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
DatabaseReference dbref;
dbref = FirebaseDatabase.getInstance().getReference().child("Account").child(user).child("cart").child(firebaseCartProductViewHolder.itemId.getText().toString());
System.out.println(firebaseCartProductViewHolder.itemId.getText().toString());
if(firebaseCartProductViewHolder.check1.isChecked()) {
dbref.child("status").setValue("check");
}
else
{
dbref.child("status").setValue("pending check");
}
}
});*/
}
@NonNull
@Override
public FirebaseCartProductViewHolder onCreateViewHolder(@NonNull ViewGroup parent,int viewType){
return new FirebaseCartProductViewHolder(LayoutInflater.from(getActivity()).inflate(R.layout.cart_view_blog_row,parent,false));
}
};
cartList.setAdapter(adapter);
return v;
}
FirebaseCartProductViewHolder.java
public class FirebaseCartProductViewHolder extends RecyclerView.ViewHolder{
public TextView itemId,productName,productPrice;
public ImageView productImage;
public CheckBox check1;
public FirebaseCartProductViewHolder(@NonNull View itemView){
super(itemView);
itemId = itemView.findViewById(R.id.itemIdTextView);
productName = itemView.findViewById(R.id.productNameTextView);
productPrice = itemView.findViewById(R.id.productPriceTextView);
productImage =itemView.findViewById(R.id.productImageView);
check1 = itemView.findViewById(R.id.checkbox1);
}
}
Cart.java
package com.example.imlproject;
public class Cart{
String itemID;
String productID;
String productName;
String description;
String price;
String quantityOrder;
String imageURL;
String status;
public Cart(){
}
public Cart(String itemID,String productName,String description,String price,String quantityOrder,String imageURL,String status, String productID){
this.productID = productID;
this.itemID=itemID;
this.productName=productName;
this.description=description;
this.price=price;
this.quantityOrder=quantityOrder;
this.imageURL=imageURL;
this.status =status;
}
public void setItemID(String itemID){
this.itemID=itemID;
}
public String getProductID(){
return productID;
}
public void setProductID(String productID){
this.productID=productID;
}
public String getStatus(){
return status;
}
public void setStatus(String status){
this.status=status;
}
public String getItemID(){
return itemID;
}
public void seItemID(String itemID){
this.itemID=itemID;
}
public String getProductName(){
return productName;
}
public void setProductName(String productName){
this.productName=productName;
}
public String getDescription(){
return description;
}
public void setDescription(String description){
this.description=description;
}
public String getPrice(){
return price;
}
public void setPrice(String price){
this.price=price;
}
public String getQuantityOrder(){
return quantityOrder;
}
public void setQuantityOrder(String quantityOrder){
this.quantityOrder=quantityOrder;
}
public String getImageURL(){
return imageURL;
}
public void setImageURL(String imageURL){
this.imageURL=imageURL;
}
}
cart_view_blog_row.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardCornerRadius="12dp"
android:elevation="90dp"
android:layout_margin="5dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#F7C9B1"
android:padding="10dp"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/productImageView"
android:layout_width="80dp"
android:layout_height="80dp"
android:scaleType="centerCrop"
android:background="#A38F84"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="5dp"
android:orientation="vertical">
<TextView
android:id="@+id/itemIdTextView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:hint="0000000-00000-000" />
<TextView
android:id="@+id/productNameTextView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:hint="food name"
android:textSize="20dp"
android:gravity="center_vertical"
android:layout_weight="2"/>
<LinearLayout
android:id="@+id/productPriceLinearLayout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<TextView
android:id="@+id/productPriceTextView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="RM0.00" />
<CheckBox
android:id="@+id/checkbox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
</androidx.cardview.widget.CardView>
How to overcome this? I guess there was a problem to save the state of the checkbox????
Aucun commentaire:
Enregistrer un commentaire