dimanche 8 décembre 2019

Check a box, multiple checkbox follow to do so

In beginning 1-5 time checking the checkbox, it was no issue happening. After that there are some checkboxes is follow check or uncheck when I check or uncheck a checkbox.

I have to some research for this question but I did not know how to implement it in my code.

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;
    }
}

How to overcome this? I guess there was a problem to save the state of the checkbox????




Aucun commentaire:

Enregistrer un commentaire