0

While implementing MVVM architecture with Data Binding and Live Data run into problem with getting error "cannot find symbol method setUser_list_user_view(User_List_UserViewModel)" I have done many time rebuild, cleanup and other stuff but this error not going..I am doing this first time so not sure have implemented the right method. Below is my code. Thnx in advance for the help

User_List_UserViewModel.java

public class User_List_UserViewModel extends AndroidViewModel {
    private User_List_UserRepository mRepository;
    private LiveData<List<User>> mAllUser;

    public User_List_UserViewModel(Application application) {
        super(application);
        mRepository = new User_List_UserRepository(application);
        mAllUser = mRepository.getmUserlist();
    }

    LiveData<List<User>> getAllWords() {
        return mAllUser;
    }

    public void insert(User user) {
        mRepository.insert(user);
    }
}

User_List_UserAdapter.java

public class User_List_UserAdapter extends RecyclerView.Adapter<User_List_UserAdapter.User_List_ViewHolder> {
    private LayoutInflater mInflater;
    private List<User_List_UserViewModel> user_list_userViewModels;

    class User_List_ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private PeopleListItemBinding mBinding;
        private TextView mFirst_NameTextView;

        public User_List_ViewHolder(PeopleListItemBinding itemBinding) {
            super(itemBinding.getRoot());
            mBinding = itemBinding;

        }

        public void bind(User_List_UserViewModel user_list_userViewModel) {
            this.mBinding.setUser_list_user_view(user_list_userViewModel);
            mBinding.executePendingBindings();

        }

        public PeopleListItemBinding getPeopleListItemBInding() {
            return mBinding;
        }

        @Override
        public void onClick(View v) {

        }
    }

    public User_List_UserAdapter(List<User_List_UserViewModel> newsList) {
        this.user_list_userViewModels = newsList;
    }

    @NonNull
    @Override
    public User_List_ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (mInflater == null) {
            mInflater = LayoutInflater.from(parent.getContext());
        }
        PeopleListItemBinding peopleListItemBinding = DataBindingUtil.inflate(mInflater, R.layout.people_list_item, parent, false);
        return new User_List_ViewHolder(peopleListItemBinding);

    }

    @Override
    public void onBindViewHolder(@NonNull User_List_ViewHolder holder, int position) {
        User_List_UserViewModel userViewModel = user_list_userViewModels.get(position);
        holder.bind(userViewModel);
    }

    @Override
    public int getItemCount() {
        if (user_list_userViewModels != null)
            return user_list_userViewModels.size();
        else return 0;
    }
}

People_List_Fragment.java

public class People_List_Fragment extends Fragment {
    List<User_List_UserViewModel> user_list = new ArrayList<>();

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup
            container, Bundle savedInstanceState) {
        final RecyclerView rv = (RecyclerView) inflater.inflate(
                R.layout.people_list, container, false);
        rv.setLayoutManager(new LinearLayoutManager(rv.getContext()));
        rv.setAdapter(new User_List_UserAdapter(user_list));
        return rv;

    }
}
Khemraj Sharma
  • 57,232
  • 27
  • 203
  • 212
Sukesh Saxena
  • 201
  • 2
  • 18

2 Answers2

1

welcome to data binding.

You have not shown your layout so I can not see what is variable name, you have taken in your layout. But here is an example, which will explain you trick.

1> Create <variable item of type User_List_UserViewModel in layout.

<layout xmlns:android="http://schemas.android.com/apk/res/android"
    >

    <data>

        <variable
            name="item"
            type="com.sample.User_List_UserViewModel"/>

    </data>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <!--other views-->

    </LinearLayout>
</layout>

2> Now you can use setItem(User_List_UserViewModel)

public void bind(User_List_UserViewModel model) {
    this.mBinding.setItem(model);
}

Dont use long or confusing variable name

<variable
     name="User_List_UserViewModel"
     type="com.sample.User_List_UserViewModel"/> 

Use short names, easy to use

<variable
     name="item"
     type="com.sample.User_List_UserViewModel"/> 

Suggestions

Update Please see this answer if you classes or variables are not generated. https://stackoverflow.com/a/51579759/6891563

Khemraj Sharma
  • 57,232
  • 27
  • 203
  • 212
  • Thanx for the suggestion. Its really helpfull – Sukesh Saxena Sep 15 '18 at 19:32
  • Still getting same error error: cannot find symbol method setItem(User_List_User) – Sukesh Saxena Sep 16 '18 at 10:26
  • Hi Khemraj, thanks for the information.I checked and went through each step carefully however my issue is not getting covered there.Getter & setter method is getting generated & can be used without error however when compiling still showing cannot find symbol method error.I am using android 3.1.4. Below is my data tag under xml layout. – Sukesh Saxena Sep 17 '18 at 00:57
  • Why you make so lengthy names?, you can make name item or model of variable. Also I suggested you read naming convention, I never seen classes name like User_List_User, what is that? Also check link of updated answer. I have already explained there why variables are not generated. – Khemraj Sharma Sep 17 '18 at 04:32
0

Could able to resolve by removing snacks from setUser_list_user. So it become setUserlistuser. This worked. As a result changed Class name from User_List_User to UserListUser. This will solve the problem. Thanx Khemraj for all the help you extended to me. Will definitely go through naming convention as well. Perhaps you can put my findings as a note in the link that you have provided me to go through. This will help beginers..

Sukesh Saxena
  • 201
  • 2
  • 18