javaandroidfirebasefirebase-realtime-databaseandroid-togglebutton

update firebase realtimeDatabase but in BottomDialog Fragment ? and also in Material Toggle group buttons


I write a code for BottomDialogFragment But the issue is when someone chooses 10₹ the program run like a loop and subtract money from his wallet continously untill it will be 0.

Main issue that was when user clicks on join that was executed continously but I never used the Loop for the code please resolve this issue I'm not able to figure out the issue

Here is My BottomRedFragment.java

public class BottomRedFragment extends BottomSheetDialogFragment {

    FragmentBottomRedBinding binding;
    ProgressDialog progressDialog;

//    FirebaseDatabase firebaseDatabase;
//    DatabaseReference databaseReference;
//    DatabaseReference price;

    int quantity = 1;
    String coin;
    int icoin = 0;
    String mobile;

    int ivalue;
    int tcmoney;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        binding = FragmentBottomRedBinding.inflate(inflater, container, false);
        View view = binding.getRoot();
        String qty = String.valueOf(quantity);
        binding.quantityRed.setText(qty);

        mobile = getActivity().getIntent().getStringExtra("mobile");

        progressDialog = new ProgressDialog(getActivity());
        progressDialog.setTitle("Joining Red");
        progressDialog.setMessage("Please Wait");

        MaterialButtonToggleGroup toggleGroup = binding.toggleButton;
        int buttonId = toggleGroup.getCheckedButtonId();
        MaterialButton button = toggleGroup.findViewById(buttonId);
        toggleGroup.addOnButtonCheckedListener(new MaterialButtonToggleGroup.OnButtonCheckedListener() {
            @Override
            public void onButtonChecked(MaterialButtonToggleGroup group, int checkedId, boolean isChecked) {

                if (group.getCheckedButtonId() == R.id.btn1) {

                    icoin = 10;
                    int ten = icoin * quantity;
                    coin = String.valueOf(ten);
                    binding.totalPriceMoney.setText(coin);
                } else if (group.getCheckedButtonId() == R.id.btn2) {

                    icoin = 50;
                    int fifty = icoin * quantity;
                    coin = String.valueOf(fifty);
                    binding.totalPriceMoney.setText(coin);
                } else if (group.getCheckedButtonId() == R.id.btn3) {

                    icoin = 100;
                    int hundred = icoin * quantity;
                    coin = String.valueOf(hundred);
                    binding.totalPriceMoney.setText(coin);
                } else {
                    Toast.makeText(getActivity(), "Please Choose a valid amount", Toast.LENGTH_SHORT).show();
                }
            }
        });
        binding.subtractRed.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                decrement();
            }
        });
        binding.addRed.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                increment();
            }
        });

        binding.joinRed.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                tcmoney = Integer.parseInt(binding.totalPriceMoney.getText().toString());
                if (tcmoney == 0 || icoin == 0) {
                    Toast.makeText(getActivity(), "Please choose Correct Values", Toast.LENGTH_SHORT).show();

                } else {
                    progressDialog.show();
                    progressDialog.setCanceledOnTouchOutside(false);
                    joinVerify();
                }
            }
        });

        return view;
    }

    private void joinVerify() {
        Toast.makeText(getContext(), mobile, Toast.LENGTH_SHORT).show();

        DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference("Users").child(mobile).child("wallet");
        databaseReference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                String value = snapshot.getValue(String.class);
                ivalue = Integer.parseInt(value);

                if (tcmoney > ivalue) {
                    progressDialog.dismiss();
                    Toast.makeText(getContext(), "Insufficient Funds", Toast.LENGTH_LONG).show();
                } else {
                    int ijoin = ivalue - tcmoney;
                    String join = String.valueOf(ijoin);

                    databaseReference.setValue(join);
                    Toast.makeText(getActivity(), "Joined Successfully", Toast.LENGTH_SHORT).show();
                    progressDialog.dismiss();


                }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {
            }
        });


    }

    private void increment() {
        quantity++;
        String s = String.valueOf(quantity);
        binding.quantityRed.setText(s);

        int calc = icoin * quantity;
        coin = String.valueOf(calc);
        binding.totalPriceMoney.setText(coin);

    }

    private void decrement() {
        quantity = quantity > 1 ? --quantity : 1;
        String s = String.valueOf(quantity);
        binding.quantityRed.setText(s);

        int calc = icoin * quantity;
        coin = String.valueOf(calc);
        binding.totalPriceMoney.setText(coin);

    }

} 

Here is my fragment_bottom_red.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    tools:context=".Fragments.BottomRedFragment">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:background="@color/red_a700"
            android:fontFamily="@font/poppins"
            android:gravity="center"
            android:text="red"
            android:textAllCaps="true"
            android:textColor="@color/white"

            android:textSize="30sp" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:background="@color/white"
            android:gravity="center"
            android:text="CONTRACT MONEY"
            android:textColor="@color/red_a700"
            android:textSize="30dp"
            android:textStyle="bold" />

        <com.google.android.material.button.MaterialButtonToggleGroup
            android:id="@+id/toggleButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="30dp"
            app:singleSelection="true">

            <Button
                android:id="@+id/btn1"
                style="?attr/materialButtonOutlinedStyle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:focusable="true"
                android:text="₹10"
                android:textColor="@color/red_a700" />

            <Button
                android:id="@+id/btn2"
                style="?attr/materialButtonOutlinedStyle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="₹50"
                android:textColor="@color/red_a700" />

            <Button
                android:id="@+id/btn3"
                style="?attr/materialButtonOutlinedStyle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="₹100"
                android:textColor="@color/red_a700" />

        </com.google.android.material.button.MaterialButtonToggleGroup>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:gravity="center"
            android:orientation="horizontal">

            <Button
                android:id="@+id/subtractRed"
                android:layout_width="wrap_content"
                android:layout_height="75dp"
                android:layout_marginEnd="30dp"
                android:backgroundTint="@color/red_a700"
                android:text="-"
                android:textColor="@color/white"
                android:textSize="30dp" />

            <TextView
                android:id="@+id/quantityRed"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="1"
                android:textColor="@color/black"
                android:textSize="60sp" />

            <Button
                android:id="@+id/addRed"
                android:layout_width="wrap_content"
                android:layout_height="75dp"
                android:layout_marginStart="30dp"
                android:backgroundTint="@color/red_a700"
                android:text="+"
                android:textColor="@color/white"
                android:textSize="30dp" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:gravity="center_horizontal"
            android:orientation="horizontal">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="20dp"
                android:text="Total order money is ₹"
                android:textColor="@color/black"
                android:textSize="20sp" />

            <TextView
                android:id="@+id/totalPriceMoney"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="20dp"
                android:text="0"
                android:textAlignment="center"
                android:textColor="@color/black"
                android:textSize="20sp" />
        </LinearLayout>

        <Button
            android:id="@+id/joinRed"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="20dp"
            android:backgroundTint="#B71C1C"
            android:fontFamily="@font/aladin"
            android:paddingStart="50dp"
            android:paddingTop="10dp"
            android:paddingEnd="50dp"
            android:paddingBottom="10dp"
            android:text="Join"
            android:textColor="@color/white"
            android:textSize="20sp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="20dp"
            android:textColor="@color/black"
            android:layout_marginBottom="30dp"
            android:fontFamily="@font/poppins"
            android:text="By clicking JOIN Button you \n agree to our presale rule"
            android:textSize="16sp"
            android:textStyle="bold" />
    </LinearLayout>

</FrameLayout>


Solution

  • OnDataChange() keep calling each time you update value in Realtime Database.

    binding.joinRed.setOnClickListener(new View.OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    tcmoney = Integer.parseInt(binding.totalPriceMoney.getText().toString());
                    if (tcmoney == 0 || icoin == 0) {
                        Toast.makeText(getActivity(), "Please choose Correct Values", Toast.LENGTH_SHORT).show();
    
                    } else {
                        progressDialog.show();
                        progressDialog.setCanceledOnTouchOutside(false);
                        joinVerify(true);
                    }
                }
            });
    
    
    
    private void joinVerify(Boolean isJoined) {
            Toast.makeText(getContext(), mobile, Toast.LENGTH_SHORT).show();
    
            DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference("Users").child(mobile).child("wallet");
            databaseReference.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot snapshot) {
                    String value = snapshot.getValue(String.class);
                    ivalue = Integer.parseInt(value);
    
                    if (tcmoney > ivalue) {
                        progressDialog.dismiss();
                        Toast.makeText(getContext(), "Insufficient Funds", Toast.LENGTH_LONG).show();
                    } else {
                        if(isJoined){
                             int ijoin = ivalue - tcmoney;
                            String join = String.valueOf(ijoin);
    
                            databaseReference.setValue(join);
                            Toast.makeText(getActivity(), "Joined Successfully", Toast.LENGTH_SHORT).show();
                            progressDialog.dismiss();
    
                            isJoined = false
    
                        }
                       
    
    
                    }
                }
    
                @Override
                public void onCancelled(@NonNull DatabaseError error) {
                }
            });
    
    
        }