0

I know this question was already asked and I followed this Question.

My Firebase structure.

enter image description here

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) {

        }
    });
droidBomb
  • 850
  • 5
  • 8

2 Answers2

0

Why not use HashSet instead of ArrayList?

Btw it's strange that you didn't get concurrent modification exception. You can't remove from the list while iterating it. Link

Set<ExpensesName> expenseClassList = new HashSet<>();

   ....

   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);
                }
   }

Here your expenseClassList will contains only unique strings

Ihor Bykov
  • 1,843
  • 3
  • 15
  • 22
-1

use the HashSet instead of ArrayList

    HashSet<String> expenseClassList = new HashSet<>();

     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()) {
                if(!expenseClassList.contains(data.child("expensesName").getValue(String.class)){
                       expenseClassList.add(spacecraft);
                    }                    
                }
                        Log.d("Tag", " list Adpter");
                        adapter.notifyDataSetChanged();
    }
Zahoor Saleem
  • 614
  • 7
  • 15