I have 5 elements in RecyclerView(elements taken from DB) with dates and texts. Two different lists. For dates and for strings. One fragment contains 1 date and 1 string text. So, im need to sort elements by date, like that
Result which i want
text1 10.09.2021
text2 13.09.2021
text3 30.09.2021
text4 1.12.2021
Result which i have
text3 30.09.2021
text4 1.12.2021
text1 10.09.2021
text2 13.09.2021
texts and dates is a two different Lists
The question is, how i can sort dates as strings(may be?) with no text loss, and where im supposed to do that (after getting from db and before retrieve data in adapter? or after loading elements in adapter and then getting them back for sort (looks bad)
thats how im get data from DB and retrieve in adapter
List<String> reminder = new ArrayList<>();
List<String> date = new ArrayList<>();
Calendar test = Calendar.getInstance();
long pars = test.getTimeInMillis();
System.out.println(pars);
dbf.child("Reminders").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
reminder.clear();
date.clear();
for(DataSnapshot child2 : snapshot.getChildren()) { // getting a data from DB to ArrayList for dropping into Adapter
for(DataSnapshot tChild : child2.getChildren()) {
if (tChild.getKey().equals("text")) {
reminder.add(tChild.getValue().toString());
rem = reminder.toArray(new String[reminder.size()]);
}
if (tChild.getKey().equals("date")) {
date.add(tChild.getValue().toString());
dat = date.toArray(new String[date.size()]);
}
}
mainRowAdapter rAdapter = new mainRowAdapter(MainActivity.this, rem,dat);
rv.setAdapter(rAdapter);
rv.setLayoutManager(new LinearLayoutManager(MainActivity.this));
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
setText in AdapterClass
holder.reminder.setText(reminder[position]);
holder.date.setText(date[position]);
in another topic i saw this one, but this cannot help me in my situation? may be some analogies exist?
Collections.sort(categories, new Comparator<Categories>() {
@Override
public int compare(Categories lhs, Categories rhs) {
return lhs.title.compareTo(rhs.title);
}
});