Skip to content Skip to sidebar Skip to footer

How To Modify An External Boolean Value In A Newly Created Listener In Java For Android

I have an Android Fragment class that has a method to write something on an external firebase database after a button is clicked. public class FR_Fragment extends Fragment implemen

Solution 1:

maybe you should not use boolean, you can use return method when the task succeeded and execute setValue() inside while(true) or for loop so when the task is successful the loop will stop working or when the loop reached to her limit. like this:

for (int i=0;i<5;i++) {
        firebase_DB.child("id").setValue("currentOrder").addOnCompleteListener(new OnCompleteListener<Void>() {
            @Override
            public void onComplete(@NonNull Task<Void> task) {
                if (task.isSuccessful()) {
                    Log.e("dbTAG", "Data successfully written.");
                    return;

                }
            }
        });
    }
    Toast.makeText(MainActivity.this, "ask for help",Toast.LENGTH_LONG);

Solution 2:

Create a custom class that extends AndroidViewModel

publicclassMyViewModelextendsAndroidViewModel{

        privateMutableLiveData<Boolean> writingSuccessful=newMutableLiveData<>();

            publicMyViewModel(@NonNullApplication application)
            {
                // you can do any other stuff here if you want
               }


            publicvoidsetBooleanWritingSuccessful(Boolean b)
            {
                writingSuccessful.setValue(b);
             }


            publicLiveData<Boolean> getBooleanWritingSuccessful()
            {
               return writingSuccessful;
             }



        }

And then use this custom viewmodel class in your fragment for observing changes to the values

publicclassFR_FragmentextendsFragmentimplementsView.OnClickListener {
       

       privateMyViewModel myViewModel;
       privateboolean writingSuccessfull= false;



                @OverridepublicvoidonCreate(Bundle savedInstanceState)
                {
                    // your stuffs// initialize viewmodel

                    myViewModel=newViewModelProvider(this,ViewModelProvider.AndroidViewModelFactory.getInstance(getActivity().getApplication())).get(MyViewModel.class);




                }





                @OverridepublicvoidonCreateView(LayoutInflater inflater, ViewGroup container,
                                             Bundle savedInstanceState)
                {
                    
                        
                        // set your viewmodel for observing the changes in boolean values.

                        myViewModel.getBooleanWritingSuccessful().observe(getViewLifecycleOwner(),newObserver<Boolean>(){

                            @OverridepublicvoidonChanged(Boolean b)
                            {

                                // update your writingSuccessful variable here.
                                writingSuccessfull=b;

                            }
                        });



                }



    // In onClick update your viewmodel's mutablelivedata;publicvoidonClick(View view) {
                    ...

                    firebase_DB.child(id).setValue(currentOrder).addOnCompleteListener(newOnCompleteListener<Void>() {
                                     @OverridepublicvoidonComplete(@NonNull Task<Void> task) {
                                         if (task.isSuccessful()) {


                                             //writingSuccessfull=true;//writingNotSuccessfull= false;//update the viewmodel's mutableLiveData 
                                             myViewModel.setBooleanWritingSuccessful(true);



                                             Log.e("dbTAG",  "Data successfully written.");

                                            } else {


                                             //writingSuccessfull=false;//writingNotSuccessfull= true;//update the viewmodel's mutableLiveData 
                                             myViewModel.setBooleanWritingSuccessful(false);


                                             Log.e("dbTAG", task.getException().getMessage());
                                            }
                                        }
                                });


                    }




                }

This is a simple snippet of the MVVM Architecture. Hope now it works. Thank you.

Post a Comment for "How To Modify An External Boolean Value In A Newly Created Listener In Java For Android"