-1

I am trying to get the ViewModel Class for the specific fragment. But ViewModelProvider.of(this).get(UserListViewModel.class) does not work for me. Extending my Fragment from "android.support.v4.app.Fragment", but still it does not work. Here is the code

package mudasir.android.application.upfit.Fragments;


import android.arch.lifecycle.AndroidViewModel;
import android.arch.lifecycle.ViewModelProvider;
import android.databinding.DataBindingUtil;
import android.os.Bundle;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import mudasir.android.application.upfit.R;
import mudasir.android.application.upfit.ViewModel.UserListViewModel;
import mudasir.android.application.upfit.databinding.FragmentUserListBinding;

/**
 * A simple {@link Fragment} subclass.
 */
public class UserListFragment extends Fragment {

    UserListViewModel userListViewModel;
    FragmentUserListBinding fragmentUserListBinding;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        fragmentUserListBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_user_list, container, false);
        View view = fragmentUserListBinding.getRoot();

        userListViewModel = ViewModelProvider.of(this).get(UserListViewModel.class);

        return view;
    }

}
4

1 回答 1

0

你使用了错误的类。它应该是:

userListViewModel = ViewModelProviders.of(this).get(UserListViewModel.class);

请注意,它是ViewModelProviders

于 2018-03-20T05:29:12.443 回答