I know this question was already asked and I followed this Question.
My Firebase structure.
I need to display expensesName in listview. If data is repeated more than once it should display the data in listview for one time only, there is no duplicates of data and if there is no duplication data in firebase, it should also be visible in listview.
I have tried the code below to get away the duplicate data but it doesn't work for data which is not repeated in firebase.
For Example in my firebase
oil // repeated 2 times
petrol //repeated 1 time
I need ouput as
oil
petrol
but actual output am getting is
oil
MainActivity
listfirebaseref=FirebaseDatabase.getInstance().getReference("Expenses Details").child(username).child(monthYr);
listfirebaseref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
expenseClassList.clear();
int sum = 0;
ExpenseClass expenseClass;
Log.d("Tag", "on list Adpter");
for (DataSnapshot ds : dataSnapshot.getChildren()) {
for (DataSnapshot data : ds.getChildren()) {
String exp = data.child("expensesName").getValue(String.class);
spacecraft = new ExpensesName(exp);
expenseClassList.add(spacecraft);
Log.d("Tag", " " + expenseClassList);
}
}
for (int i = 0; i < expenseClassList.size(); i++) {
for (int j = 0; j < expenseClassList.size(); j++) {
if ((expenseClassList.get(i).equals(expenseClassList.get(j)))) {
expenseClassList.remove(j);
//j;
}
}
}
Log.d("Tag", " list Adpter");
adapter.notifyDataSetChanged();
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});