问题
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;
}
}
回答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
- See java naming convensions https://www.geeksforgeeks.org/java-naming-conventions/
- You could make model name just
UserModel
, why so long name? - You can make
UserAdapter
, again why a confusing name.
Update Please see this answer if you classes or variables are not generated. https://stackoverflow.com/a/51579759/6891563
回答2:
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..
来源:https://stackoverflow.com/questions/52313158/cannot-find-symbol-method-setuser-list-user-viewuser-list-userviewmodel-though