如何使用viewmodel从另一个片段访问函数

问题描述 投票:0回答:1

我的一个片段中有一个函数,用于检查cardview是否已更改颜色,现在我想使用视图模型体系结构访问另一个片段的函数,我知道我必须使用viewmodel才能访问它,但是感到困惑,我该怎么做,

这是我的片段A的功能

public boolean checkIthestudentiscleared()
    {


        //get the color of the cardview and check which color is it if #2b434f then he /she is cleared
        int background_colorbursar = cardViewbusar.getCardBackgroundColor ().getDefaultColor ();
        int background_colorsnal = cardView.getCardBackgroundColor ().getDefaultColor ();
        int background_colorsuahab = cardViewsuahub.getCardBackgroundColor ().getDefaultColor ();
        int background_colorsports = cardViewsports.getCardBackgroundColor ().getDefaultColor ();
        int background_colorcict = cardViewcict.getCardBackgroundColor ().getDefaultColor ();
        int background_colorict_service = cardViewitcservices.getCardBackgroundColor ().getDefaultColor ();
        int background_coloreudtech = cardViewedutech.getCardBackgroundColor ().getDefaultColor ();
        int background_coloritcb = cardViewitcb.getCardBackgroundColor ().getDefaultColor ();


                if(background_colorbursar == Color.parseColor ("#ffffff") || background_colorcict == Color.parseColor ("#ffffff")
                || background_colorict_service == Color.parseColor ("#ffffff") || background_coloritcb == Color.parseColor ("#ffffff")
                || background_colorsnal == Color.parseColor ("#ffffff")  || background_coloreudtech == Color.parseColor ("#ffffff")  ||
                background_colorsuahab == Color.parseColor ("#ffffff") || background_colorsports == Color.parseColor ("#ffffff")){


                    Toast.makeText (getContext (), "Your not Cleared can't create the Clearance Report!", Toast.LENGTH_SHORT).show ();

                    return false;

        }

                else
                {
                    Toast.makeText (getContext (), "The dean Has Approved your Clearance!", Toast.LENGTH_SHORT).show ();
                    checkClearanceStatusViewModel.setValue ();
                    return true;



                }



    }

这是我的视图模态类

public class CheckClearanceStatusViewModel extends AndroidViewModel {


       private MutableLiveData<Boolean> CheckifCleared = new MutableLiveData<> ();

    public CheckClearanceStatusViewModel(@NonNull Application application) {
        super (application);
    }


    public void setValue()
   {
      CheckifCleared.setValue (true);
   }


    public LiveData<Boolean> getValue(){
        return CheckifCleared;
    }

}

刚刚尝试这样做,但是缺少逻辑,我希望能够从其他片段中检查该功能的有效性

在我的具有功能的片段上

 public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated (view, savedInstanceState);
        //init the viewmodel
        checkClearanceStatusViewModel = new ViewModelProvider (this).get (CheckClearanceStatusViewModel.class);
        checkClearanceStatusViewModel.getValue ().observe (getActivity (), new Observer<Boolean> () {
            @Override
            public void onChanged(Boolean aBoolean) {
                //logic here



            }
        });
    }
android android-fragments android-viewmodel
1个回答
0
投票

在要检查有效性的片段中,添加以下代码

viewModel.getValue().observe(getViewLifecycleOwner(), new Observer<Boolean>() {
            @Override
            public void onChanged(Boolean value) {
             //your logic here
            }
        });
© www.soinside.com 2019 - 2024. All rights reserved.