Can anyone show me what have I done wrong here, I am using recyclerView
with livedata every time I launch the app it spawns with a random list and sometimes its full of duplicates. Here is the code that assigns data to the adapter:
adapter = new PaymentsByDateParentAdapter(getContext());
recyclerView.setAdapter(adapter);
viewModel = new ViewModelProvider(this).get(SchoolViewModel.class);
viewModel.getAllPayments().observe(getViewLifecycleOwner(), new Observer<List<Payment>>() {
@Override
public void onChanged(List<Payment> payments) {
for (Payment i : payments) {
datesWithDupes.add(i.getPaymentDate());
}
for (String i : datesWithDupes) {
if (!datesWithoutDupes.contains(i)) {
datesWithoutDupes.add(i);
}
}
for (String date : datesWithoutDupes) {
viewModel.getPaymentsByDate(date).observe(getViewLifecycleOwner(), new Observer<List<Payment>>() {
@Override
public void onChanged(List<Payment> payments) {
PaymentByDate paymentByDate = new PaymentByDate(date, payments);
paymentByDateList.add(paymentByDate);
adapter.setPayments(paymentByDateList);
}
});
}
}
});
Clear the arraylists datesWithDupes
, paymentByDateList
and datesWithoutDupes
before the for loops.
datesWithDupes.clear();
datesWithoutDupes.clear();
paymentByDateList.clear();
for (Payment i : payments) {
datesWithDupes.add(i.getPaymentDate());
}
for (String i : datesWithDupes) {
if (!datesWithoutDupes.contains(i)) {
datesWithoutDupes.add(i);
}
}
for (String date : datesWithoutDupes) {
viewModel.getPaymentsByDate(date).observe(getViewLifecycleOwner(), new Observer<List<Payment>>() {
@Override
public void onChanged(List<Payment> payments) {
PaymentByDate paymentByDate = new PaymentByDate(date, payments);
paymentByDateList.add(paymentByDate);
adapter.setPayments(paymentByDateList);
}
});
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.