How to delete duplicated items in Listview filled with objects from real-time database

后端 未结 2 1637
庸人自扰
庸人自扰 2021-01-26 03:20

I am fetching data from my database based on the userId then I insert the data in a listview and show it through a dialog.

The behavior I was w

相关标签:
2条回答
  • 2021-01-26 03:45

    Problem solved. the reason was that the if statement is adding the entire data in swapdetails object if it found a user ID then it found another one and added all of them again etc.

    so just I have created bloolean inside the if statement and make it take true if it found swaper ID and then use the adapter outside the scope of the addChildEventListene.

     shiftSwapDb.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            if (dataSnapshot.exists()) {
                SwapDetails swapDetails = dataSnapshot.getValue(SwapDetails.class);
                if (swapDetails.getSwapperID().equals(fromID)) {
    
    hasSwaperID = true;
                }
            }
        }
    

    shiftProfileAdapter.add(swapDetails);

    0 讨论(0)
  • 2021-01-26 04:04

    try this

    @Override
    public void onChildAdded(DataSnapshot dataSnapshot, String s) {
        if (dataSnapshot.exists()) {
            SwapDetails swapDetails = dataSnapshot.getValue(SwapDetails.class);
            if (swapDetails.getSwapperID().equals(fromID)) {
                swapBodyList.add(swapDetails);
                shiftProfileAdapter.notifyItemInserted(swapBodyList.length - 1);
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题