I am trying to use MVVM, I am trying to get the data in my Viewodel ( normally coming from a service ) and set them to my activity, but it seems like it does not works, when I try just a simple ArrayList of ContractModel and add it to my adapters, everything works fine, but when I try to call my ViewModel things stops working.
I am folowing a course that is doing the same things as I did but not having the same result.
This is my MainActivity :
public class MainActivity extends AppCompatActivity {
ContractsListViewModel contractsListViewModel;
@BindView(R.id.contractList)
RecyclerView contractList;
RecyclerView.Adapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
contractList.setLayoutManager(new LinearLayoutManager(this));
contractsListViewModel = ViewModelProviders.of(this).get(ContractsListViewModel.class);
contractsListViewModel.call();
adapter = new ContractListAdapter(this, contractsListViewModel.contractList.getValue());
contractList.setAdapter(adapter);
}
}
That is my ViewModel :
public class ContractsListViewModel extends ViewModel {
public MutableLiveData<List<ContractModel>> contractList;
MutableLiveData<Boolean> isLoading;
MutableLiveData<Boolean> error;
public void call(){
fetchContracts();
}
public void fetchContracts(){
ContractModel contractModel = new ContractModel(1, "hfdfd", "h");
ContractModel contractModel1 = new ContractModel(1, "hfdffddd", "h");
ContractModel contractModel2 = new ContractModel(1, "hdffd", "j");
ContractModel contractModel3 = new ContractModel(1, "hdffdfd", "h");
List<ContractModel> list = new ArrayList<ContractModel>();
list.add(...allofthem);
contractList.setValue(list);
}
}
My Adapter is working fine since it worked with the ArrayList I mentionned above. But here is :
public class ContractListAdapter extends RecyclerView.Adapter<ContractListAdapter.ContractViewHolder> {
List<ContractModel> contracts;
Context context;
public ContractListAdapter(Context context, List<ContractModel> contracts){
this.context = context;
this.contracts = contracts;
}
@NonNull
@Override
public ContractViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.contrat, parent, false);
return new ContractViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ContractViewHolder holder, int position) {
holder.bind(contracts.get(position));
}
@Override
public int getItemCount() {
return this.contracts.size();
}
public class ContractViewHolder extends RecyclerView.ViewHolder {
@BindView(R.id.courtier)
TextView courtier;
public ContractViewHolder(@NonNull View view){
super(view);
ButterKnife.bind(this, view);
}
public void bind(ContractModel contractModel){
courtier.setText(contractModel.getCourtier());
}
}
}
Any help would be much appreciated.