I am fetching data from Cloud Firestore and displaying in a recyclerview using MVVM architecture along with LiveData. When I observe the changes in viewmodel and notify the adapter about the dataset change, the changed data isn't updating in the recyclerview.
Do you see anything wrong in the code?
UPDATE: I have updated code as suggested my the first answer. But still no success. I observed that even after the repository fetches the data from firestore, it doesn't updates the viewmodel about it. I have added the repository class too. Do you see any problem there?
HomeActivity.java
public class HomeActivity extends AppCompatActivity {
RecyclerView recyclerView;
StandardPlansAdapter adapter;
HomeActivityViewModel viewModel;
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.recyclerViewHome);
viewModel = new ViewModelProvider(this).get(HomeActivityViewModel.class);
adapter = new StandardPlansAdapter(viewModel.getStandardPlans().getValue());
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(adapter);
viewModel.getStandardPlans().observe(this, plans -> {
adapter.setStandardPlans(plans);
adapter.notifyDataSetChanged();
});
}
}
StandardPlansAdapter.java
public class StandardPlansAdapter extends RecyclerView.Adapter<StandardPlansAdapter.StandardPlansViewHolder> {
private ArrayList<Plan> standardPlans;
public StandardPlansAdapter(ArrayList<Plan> standardPlans) {
this.standardPlans = standardPlans;
}
@NonNull
@Override
public StandardPlansViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.home_rv_layout, parent, false);
return new StandardPlansViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull StandardPlansViewHolder holder, int position) {
Plan plan = standardPlans.get(position);
holder.textView.setText(plan.getName());
}
@Override
public int getItemCount() {
return standardPlans.size();
}
class StandardPlansViewHolder extends RecyclerView.ViewHolder {
TextView textView;
public StandardPlansViewHolder(@NonNull View itemView) {
super(itemView);
textView = itemView.findViewById(R.id.tvPlanName);
}
}
public void setStandardPlans(ArrayList<Plan> standardPlans) {
this.standardPlans = standardPlans;
}
}
HomeActivityViewModel.java
public class HomeActivityViewModel extends ViewModel {
private StandardPlansRepository repository;
private MutableLiveData<ArrayList<Plan>> standardPlans;
public HomeActivityViewModel() {
super();
repository = StandardPlansRepository.getInstance();
standardPlans = repository.getStandardPlans();
}
public LiveData<ArrayList<Plan>> getStandardPlans() {
return standardPlans;
}
}
StandardPlansRepository.java
public class StandardPlansRepository {
private static StandardPlansRepository instance;
private ArrayList<Plan> standardPlans = new ArrayList<>();
public static StandardPlansRepository getInstance() {
if (instance == null) {
instance = new StandardPlansRepository();
}
return instance;
}
public MutableLiveData<ArrayList<Plan>> getStandardPlans() {
setStandardPlans();
MutableLiveData<ArrayList<Plan>> plans = new MutableLiveData<>();
plans.setValue(standardPlans);
return plans;
}
private void setStandardPlans() {
documentReference.get().addOnSuccessListener(documentSnapshot -> {
if (documentSnapshot.exists()) {
StandardPlans plans = documentSnapshot.toObject(StandardPlans.class);
if (plans != null) {
standardPlans = plans.getStandard_plans();
}
} else {
Log.e("rahul", "Document doesn't exist");
}
}).addOnFailureListener(e -> {
Log.e("rahul", e.getMessage());
e.printStackTrace();
});
}
}