本文介绍了如何使用Firebase Android在列表视图中删除重复数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 同学,我得到了一个头缓存,以了解如何解决使用Firebase删除列表视图中的重复数据的问题.我只需要1个项目数据即可显示在列表视图中.在屏幕截图中显示为示例 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) { } }); //*************************************23/03/2018 **** **************** ATTEMP 1 //VARIABLESArrayList<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); } 非常感谢您的评论和解决方案 推荐答案 @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(); 这篇关于如何使用Firebase Android在列表视图中删除重复数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-22 09:43