Skip to content Skip to sidebar Skip to footer

How To Remove Duplicate Data In List View With Firebase Android

** SOLVED **** SOLUTION IN BELOW POST ******* SOLUTION IN BELOW POST ******* ** Classmate's i get a headcache to how resolve to remove duplicate data in a listview with firebase.

Solution 1:

Create a new List, do for loop over the old one and for each item in the old one, check if the new list contains the item, if it doesn't, add it to the new list.

This works if you have a good hashCode method on your model, if you don't, your two identical objects might have different hashCodes, thus making this technique (and other techniques that use hashCodes) ineffective.

In that case you either have to override the model's hashCode or if they have a unique field like id, use that to check for equality. (in the for loop above, for each item, do another for loop on the new list and check if none of the items in the new list does not equal the item's id, add it to the list.)

Solution 2:

SOLUTION (WORK FINE):

 @Override
        publicvoidonDataChange(DataSnapshot dataSnapshot) {
            for(DataSnapshot data : dataSnapshot.getChildren()) {
                ShowCliente show = data.getValue(ShowCliente.class);
                myList.add(show);
                System.out.println("Duplicate:"+myList);
            }

            for(int i=0; i < myList.size(); i++){
                for(int j=0; j < myList.size(); j++){
                    if(myList.get(i).equals(myList.get(j))){
                        myList.remove(j);
                        //j;
                    }
                }
            }
            arrayAdapter.notifyDataSetChanged();
            checkEmpty();

Post a Comment for "How To Remove Duplicate Data In List View With Firebase Android"