简体   繁体   中英

Click Listener of button from the XML of a fragment that is MVVM based architecture not working

Don't Know why my logout button is not working from the XML via data-binding.

ProfileFragment.java

This is simply the fragment for viewing the profile.

 public class ProfileFragment extends Fragment {

private ProfileViewModel profileViewModel;
private FragmentProfileBinding binding;

public View onCreateView(@NonNull LayoutInflater inflater,
                         ViewGroup container, Bundle savedInstanceState) {
    profileViewModel =
            new ViewModelProvider(requireActivity()).get(ProfileViewModel.class);

    binding = FragmentProfileBinding.inflate(inflater, container, false);
    binding.setLifecycleOwner(this);
    View root = binding.getRoot();
    //        binding.btnLogout.setOnClickListener(v -> {
    ////            logout(v); from here it works but I want to perform this from XML
    //        });
    profileViewModel.getModelLiveData().observe(getViewLifecycleOwner(), new 
    Observer<UserModel>() {
        @Override
        public void onChanged(UserModel profileViewModel) {
            binding.username.setText(profileViewModel.getUsername());
            binding.email.setText(profileViewModel.getEmail());
            binding.phone.setText(profileViewModel.getPhone());
            binding.cnic.setText(profileViewModel.getCnic());
            binding.adress.setText(profileViewModel.getAdress());
        }
    });
    return root;
}

public void logout(View view) {
    Toast.makeText(view.getContext(), "logout clicked", Toast.LENGTH_SHORT).show();
    FirebaseAuth.getInstance().signOut();
    Intent intent = new Intent(view.getContext(), Login.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    view.getContext().startActivity(intent);
}

@Override
public void onDestroyView() {
    super.onDestroyView();
    binding = null;
}
}

ProfileViewModel.java

Profile View model is previously populated then make the XML to view that text

public class ProfileViewModel extends ViewModel {

public MutableLiveData<String> username = new MutableLiveData<>();
public MutableLiveData<String> email = new MutableLiveData<>();
public MutableLiveData<String> cnic = new MutableLiveData<>();
public MutableLiveData<String> password = new MutableLiveData<>();
public MutableLiveData<String> phone = new MutableLiveData<>();
public MutableLiveData<String> adress = new MutableLiveData<>();
public MutableLiveData<String> img = new MutableLiveData<>();

private MutableLiveData<UserModel> userMutableLiveData;

public MutableLiveData<UserModel> getModelLiveData() {

    if (userMutableLiveData == null) {
        userMutableLiveData = new MutableLiveData<>();
    }
    return userMutableLiveData;
}

public ProfileViewModel() {
    UserModel userModel = UserAfterLogin.getUserModel();
    getModelLiveData().setValue(userModel);

}

//the following method needs to be in working with the fragment xml click listener
//    android:onClick="@{(v) -> ProfileFragment.logout(v)}"
public void logout(View view) {
    Toast.makeText(view.getContext(), "clicked", Toast.LENGTH_SHORT).show();
    FirebaseAuth.getInstance().signOut();
    Intent intent = new Intent(view.getContext(), Login.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    view.getContext().startActivity(intent);
}

}

fragment_profile.xml

The XML of my fragments in which the main issue occurs.

android:onClick="@{(v) -> ProfileFragment.logout(v)}"

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<data>

    <variable
        name="ProfileFragment"
        type="com.fyp.utilitymoan.ui.profile.ProfileViewModel" />
</data>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:background="@drawable/background"
    android:gravity="center"
    android:orientation="vertical"
    tools:context=".ui.profile.ProfileFragment">

    <de.hdodenhof.circleimageview.CircleImageView 
     xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/profile_image"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:layout_margin="10dp"
        android:src="@drawable/ic_baseline_person_24"
        app:civ_border_color="@color/white"
        app:civ_border_width="2dp" />

    <EditText
        android:id="@+id/username"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_gravity="center"
        android:layout_margin="5dp"
        android:background="@drawable/btn_background"
        android:gravity="center"
        android:hint="Username"
        android:text="@={ProfileFragment.username}"
        android:textColor="@color/white" />

    <EditText
        android:id="@+id/email"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_gravity="center"
        android:layout_margin="5dp"
        android:background="@drawable/btn_background"
        android:gravity="center"
        android:hint="Email"
        android:text="@={ProfileFragment.email}"
        android:textColor="@color/white" />


    <EditText
        android:id="@+id/cnic"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_gravity="center"
        android:layout_margin="5dp"
        android:background="@drawable/btn_background"
        android:gravity="center"
        android:hint="CNIC"
        android:inputType="numberDecimal"
        android:text="@={ProfileFragment.cnic}"
        android:textColor="@color/white" />

    <EditText
        android:id="@+id/phone"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_gravity="center"
        android:layout_margin="5dp"
        android:background="@drawable/btn_background"
        android:gravity="center"
        android:hint="Phone"
        android:inputType="numberDecimal"
        android:text="@={ProfileFragment.phone}"
        android:textColor="@color/white" />


    <EditText
        android:id="@+id/adress"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_gravity="center"
        android:layout_margin="5dp"
        android:background="@drawable/btn_background"
        android:gravity="center"
        android:hint="Address"
        android:text="@={ProfileFragment.adress}"
        android:textColor="@color/white" />


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_margin="10dp"
        android:gravity="center"
        android:orientation="horizontal">


        <Button
            android:id="@+id/btnLogout"
            android:layout_width="120dp"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:background="@drawable/btn_background"
            android:clickable="true"
            android:fontFamily="serif"
            android:onClick="@{(v) -> ProfileFragment.logout(v)}"
            android:padding="12dp"
            android:text="Logout"
            android:textAllCaps="false"
            android:textColor="@color/white"
            android:textStyle="bold" />


        <Button
            android:layout_width="120dp"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:background="@drawable/btn_background"
            android:fontFamily="serif"
            android:padding="12dp"
            android:text="Save"
            android:textAllCaps="false"
            android:textColor="@color/white"
            android:textStyle="bold" />


    </LinearLayout>


</LinearLayout>
</layout>

If you want to call viewmodel method from xml using data binding,first you need to set viewmodel object for xml in your fragment.

profileViewModel =
            new ViewModelProvider(requireActivity()).get(ProfileViewModel.class);
    binding = FragmentProfileBinding.inflate(inflater, container, false);
    binding.setLifecycleOwner(this);
    binding.setProfileFragment(profileViewModel);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM