My app loads items from a list that contains categories and other items
The list is like this with the names of repeated categories
What I want to do is load only the category names without repeating an existing name
Example: TV Shows, Space, Nature, Cars
My Cod:
DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference("Data");
Query query = databaseReference.orderByChild("category");
query.addValueEventListener(new ValueEventListener() {
@SuppressLint("NotifyDataSetChanged")
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
// dataList.clear();
for (DataSnapshot dataSnapshot : snapshot.getChildren()) {
ModelCatMenu dataClass = dataSnapshot.getValue(ModelCatMenu.class);
dataList.add(dataClass);
}
adapter.notifyDataSetChanged();
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
Data:
{
"Data": {
"Abstract": {
"-Nw0kYmCYjEApQYc_iew": {
"category": "Abstract",
"imageLink": "https://link",
"name": "name"
}
},
"Filme": {
"-NvyyIvCLDveuyUHP0I4": {
"category": "Filme",
"imageLink": "https://link",
"name": "Cong"
},
"-NvyyP8qn9IhDJVoNcEk": {
"category": "Filme",
"imageLink": "https://link",
"name": "name"
}
},
"Tv Shows": {
"-Nw0kkdorKFS0oi2N3gj": {
"category": "Tv Shows",
"imageLink": "https://link",
"name": "name"
}
}
}
}
I tried this but without success, something is missing
List<String> uniqueCategories = new ArrayList<>();
query.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
Category category = snapshot.getValue(Category.class);
String categoryName = category.getCategory();
if (!uniqueCategories.contains(categoryName)) {
uniqueCategories.add(categoryName);
}
}
adapter.notifyDataSetChanged();
}
@Override
public void onCancelled(DatabaseError databaseError) {
// Trate o erro, se necessário
}
});