Sukesh Saxena
Sukesh Saxena

Reputation: 211

Creating RecycleView Adaptor using DataBinding and ViewModel through MVVM approach

I want to display data in RecyclerListview with the help of Data Binding & ViewModel using MVVM approach. In my XML have created data variable of View Model class however I am getting error in my adaptor class on the statement

holder.chatlistitemBinding.setChatlistVM(mChatList.get(position)); 

since mChatList.get(position) is array of ChatList Model and not of ViewModel. How can I correct it? Please dont advise to change data varriable pointing to Model Class(ChatList). I want to implement MVVM approach through View Model. Thanks in advance.

My XML File:

<?xml version="1.0" encoding="utf-8"?>

<layout xmlns:android="http://schemas.android.com/apk/res/android"
>
    <data >
    <variable name="handlers" type="com.support.android.designlibdemo.View.Handlers.ChatListHandlers"/>

      <variable name="chatlistVM" type="com.support.android.designlibdemo.ViewModel.ChatListViewModel" />
    </data>
    <RelativeLayout
    .......

            <TextView
                android:id="@+id/placeholder2"

                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@{chatlistVM.chatList.first_name}"
                android:textAppearance="?android:attr/textAppearanceSmall" 
      />
            ...
</layout>

Adaptor Class:

public class AdapterChatList extends RecyclerView.Adapter<AdapterChatList.ChatListViewHolder> {
    private LayoutInflater mInflater;
    private static final String TAG = "ChatListAdapter";
    public List<ChatListViewModel> mChatList = new ArrayList<>();
    public AdapterChatList(Context context) {
    mInflater = LayoutInflater.from(context);
    }
    class ChatListViewHolder extends RecyclerView.ViewHolder{

    private ChatlistitemBinding chatlistitemBinding;
    public ChatListViewHolder(ChatlistitemBinding itemBinding) {
        super(itemBinding.getRoot());
        chatlistitemBinding = itemBinding;
    }

    }

    @NonNull
    @Override
    public ChatListViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    if (mInflater == null) { mInflater = LayoutInflater.from(parent.getContext()); }
    ChatlistitemBinding chatListUserItemBinding = DataBindingUtil.inflate(mInflater, R.layout.chatlistitem, parent, false);
    return new ChatListViewHolder(chatListUserItemBinding);
    }
    @Override
    public void onBindViewHolder(@NonNull ChatListViewHolder holder, int position) {
    holder.chatlistitemBinding.setChatlistVM(mChatList.get(position));
    holder.chatlistitemBinding.setHandlers(new ChatListHandlers());
    holder.chatlistitemBinding.executePendingBindings();
    }
    @Override
    public int getItemCount() {
    if (mChatList != null)
        return mChatList.size();
    else return 0;
    }

    void add(ChatList chatList) {
        mChatList.add(chatList);
        notifyItemInserted(getItemCount());
    }
    public void updatelist(List<ChatList> userlist){//Need to rethink on this method
    mChatList = userlist;
    notifyDataSetChanged();
    }
}

ViewModel:

public class ChatListViewModelextends AndroidViewModel {

    private ChatList chatList ;

    public ChatListViewModel(Application application){
        super(application);
        chatList = new ChatList();

    }

}

Upvotes: 0

Views: 258

Answers (2)

R&#243;bert Nagy
R&#243;bert Nagy

Reputation: 7622

Factory class:

  @Suppress("UNCHECKED_CAST")
class Factory @Inject constructor(
    private val chatList: ChatList
) : ViewModelProvider.Factory {

    override fun <T : ViewModel?> create(modelClass: Class<T>): T {
        if (modelClass.isAssignableFrom(ChatListViewModel::class.java)) {
            return ChatListViewModel(chatList) as T
        }
        throw IllegalArgumentException("Unknown ViewModel class: ${modelClass.canonicalName}")
    }

}

Then you get your VM the same way, you just need to pass the Factory instance For example from your Fragment or FragmentActivity you'd call:

viewModel = ViewModelProviders.of(this, viewModelFactory).get(ChatListViewModel:class.java);

Upvotes: 0

R&#243;bert Nagy
R&#243;bert Nagy

Reputation: 7622

I wouldn't extend the ViewModel class provided by the framework in this case, since you want to tie the ViewModel to a specific view only.

So the solution is simple: Have chatList as a constructor parameter in your VM, then set it like holder.chatlistitemBinding.setChatlistVM(new ChatListViewModel(mChatList.get(position)));

Upvotes: 1

Related Questions