0

** 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. I need only 1 item data to show in listview. Showing to example in screen shot

enter image description here

Actual Code:

  ArrayList<ShowCliente> myList = new ArrayList<>();

    final ArrayAdapter<ShowCliente> arrayAdapter = new ArrayAdapter<>(getActivity(),android.R.layout.simple_list_item_1, myList);

   newListView.setAdapter(arrayAdapter);
 gDatabase.child("cliente").addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            ShowCliente show = dataSnapshot.getValue(ShowCliente.class);
            myList.add(show);
            //myList.clear();

            //*********************************
            arrayAdapter.notifyDataSetChanged();
            checkEmpty();
        }

        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {


        }

        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {

        }

        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

Last trying code: This my attemps on this day to resolve this problem, any Don't work, post it to you knowledge :(

        //*************************************23/03/2018 **** **************** ATTEMP 1

    //VARIABLES
ArrayList<ShowCliente> myList = new ArrayList<>();
List<ShowCliente> myShow = new ArrayList<>();
ArrayList<ShowCliente> mList1 = new ArrayList<>(new HashSet<ShowCliente>(myList));
//HashSet<ShowCliente> myHash = new HashSet<>();

     //LEE TODOS DATOS DE LOS CHILD DE LA BASE DE DATOS
          ShowCliente show  = dataSnapshot.getValue(ShowCliente.class);
          //DISMINUIR A 1 LOS CLIENTES REPETIDOS EN LISTA
            Iterator<ShowCliente> iteShow = myList.iterator();
            while(iteShow.hasNext()){
                ShowCliente ite = iteShow.next();
                if(ite.equals(show)) iteShow.remove();
            }
            myList.add(show);

          //ShowCliente key = dataSnapshot.getKey();
          //mKeys.add(key);
          arrayAdapter.notifyDataSetChanged();
          checkEmpty();


    //****************************************************************** ATTEMP 2

    for(DataSnapshot shot : dataSnapshot.getChildren()){
                ShowCliente show = shot.getValue(ShowCliente.class);
                    String compare = String.valueOf(show.getRazonsoc());
                   if(show.getRazonsoc() != compare){
                       myList.add(show);
                   }
            }


    //****************************************** ATTEMP 3

       ShowCliente show = dataSnapshot.getValue(ShowCliente.class);
            //myList.clear();
            myList.add(show);
            if(myList.indexOf(show) == myList.lastIndexOf(show)){
                myList.clear();

            }

            arrayAdapter.notifyDataSetChanged();
            checkEmpty();      

   //************************************** ATTEMP 4

    ShowCliente show = dataSnapshot.getValue(ShowCliente.class);

            myShow.add(show);

            HashSet<ShowCliente> hashSet = new HashSet<>();
            hashSet.addAll(myShow);
            myShow.clear();
            myShow.addAll(hashSet);

   //************************************ ATTEMP 5 

             for(DataSnapshot shot : dataSnapshot.getChildren()){
                ShowCliente show = shot.getValue(ShowCliente.class);
                String value = show.getRazonsoc();
                stringList.clear();
                stringList.add(value);

            }

Gratefully for your comments and solutions

Ignacio_aa
  • 318
  • 2
  • 8
  • Check if the object is in your list or not. –  Mar 23 '18 at 20:11
  • @Ibrahim i need remove the data not checking if exist or not – Ignacio_aa Mar 23 '18 at 20:20
  • Please add your current code and delete your other coding attemps, its currently very unclear... – julienduchow Mar 23 '18 at 20:22
  • And I do not really know what you are trying to do, if you want to display only the last received item, then just call myList.clear() and then AFTER that myList.add(show) – julienduchow Mar 23 '18 at 20:25
  • https://github.com/firebase/FirebaseUI-Android/blob/master/database/README.md – Linxy Mar 23 '18 at 20:30
  • You can try using a hash and set the key of the hash to the items String value. And then before adding any new item check if that key exists and if it does not exists then add or else do not add. This way you will have a unique list. – Rahulrr2602 Mar 23 '18 at 21:33

2 Answers2

0

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

Adib Faramarzi
  • 3,798
  • 3
  • 29
  • 44
0

SOLUTION (WORK FINE):

 @Override
        public void onDataChange(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();
Ignacio_aa
  • 318
  • 2
  • 8