How to remove duplicate data in list view with firebase android

一曲冷凌霜 提交于 2020-01-06 06:40:25

问题


** 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

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


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




回答2:


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


来源:https://stackoverflow.com/questions/49457529/how-to-remove-duplicate-data-in-list-view-with-firebase-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!