Search code examples
androiddata-bindingonclicklistener

android data binding error using listener binding


I'm binding a listener in my View model to the on-click of a FAB in my fragment xml.

My fragment xml showing the FAB.

 <data>
        <variable
            name="vehicleViewModel"
            type="com.buildit4me.autonance.ui.VehicleViewModel" />
        <variable
            name="launcher"
            type="androidx.activity.result.ActivityResultLauncher" />

 <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fabVehicles"
        android:contentDescription="@string/fab_content_description"
        style="@style/FAB"
        android:onClick="@{(view) -> vehicleViewModel.FabOnClick(view,launcher)}"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent" />

My Viewmodel listener

  public void FabOnClick( View view,  ActivityResultLauncher<Intent> launcher){
         Intent intent= new Intent(view.getContext(), AddNewEntityActivity.class);
         intent.putExtra(getApplication().getResources().getString(R.string.fragmentID), R.layout.fragment_add_vehicle);
         launcher.launch(intent);
     }

In My fragment

 ActivityResultLauncher<Intent> launcher= registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<>() {
        @Override
        public void onActivityResult(ActivityResult result) {
              if(result.getResultCode()== Activity.RESULT_OK){
                  mVehicleViewModel.getAllVehicles();
              }
             String msg;
              try {
                   msg =  result.getData().getStringExtra(getResources().getString(R.string.intent_msg));
              }
              catch( NullPointerException e){
                  msg=null;
              }
            Utils.displaySnackbar(binding.recyclerviewVehicles,binding.fabVehicles,msg);
        }
    });

public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        binding= DataBindingUtil.inflate(inflater,R.layout.fragment_vehicles,container,false);        
        mVehicleViewModel = new ViewModelProvider(requireActivity()).get(VehicleViewModel.class);
        binding.setVehicleViewModel(mVehicleViewModel);
        binding.setLauncher(launcher);

The error when I build the project.

[databinding] {"msg":"cannot find method FabOnClick(android.view.View, androidx.activity.result.ActivityResultLauncher) in class com.buildit4me.autonance.ui.VehicleViewModel","file":"app/src/main/res/layout/fragment_vehicles.xml","pos":[{"line0":30,"col0":37,"line1":30,"col1":78}]}

I tried invalidating the cache to clear the generated binding files but still got the error.


Solution

  • Thanks to viethoang for pointing out the source of the problem.

    I changed the fragment xml variable declaration

        <variable
                name="launcher"
                type="androidx.activity.result.ActivityResultLauncher&lt;Intent&gt;"/>
    

    I had to escape the "<" and ">". I found this on google's site showing they were doing the same thing when declaring generics in xml. Observable collections