如何从实时数据库中删除填充有对象的Listview中的重复项 [英] How to delete duplicated items in Listview filled with objects from real-time database

查看:39
本文介绍了如何从实时数据库中删除填充有对象的Listview中的重复项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在基于userId从数据库中获取数据,然后将数据插入listview中并通过对话框显示.

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.

我一直在等待的行为是,我将获得用户创建的所有交换并将其插入列表中,以便他可以选择其中之一.但是,仅当他在数据库中只有一个交换(如通常显示的那样)时,该代码才能正常工作.但是,如果他有两次交换,则列表中的交换将被乘以2.

The behavior I was waiting for is is that I will get all the swaps the user has created and insert them in the list so he can choose one of them. But the code only works fine if he has only one swap in the database as it appears normally. But if he has two swaps then the swaps inside the list will be multiplied in two.

如果是3,则数据将重复3次,依此类推.我不知道我的代码在这里的流程是什么,希望有人可以帮助我解决这个问题.如何摆脱列表中的重复项?

And if it's three then the data will get repeated three times and so on. I don't know what is the flow in my code here and hope there is someone who can help me in this problem. how can I get rid of the duplicated items in the list?

 private void fetchChooseList() {

    DatabaseReference shiftSwapDb = FirebaseDatabase.getInstance().getReference().child("swaps").child("shift_swaps");

    final List<SwapDetails> swapBodyList = new ArrayList<>();
    Collections.reverse(swapBodyList);
    shiftProfileAdapter = new ShiftProfileAdapter(ProfileActivityShift.this, R.layout.shift_profile_list_item, swapBodyList);
    listView = chooseShiftProfileDialog.findViewById(R.id.listShiftProfileChooseDialog);
    listView.setAdapter(shiftProfileAdapter);

    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)) {
                    shiftProfileAdapter.add(swapDetails);
                }
            }
        }
        @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) { }
    });


    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            progressBar_ShiftProfileChooseDialog.setVisibility(View.VISIBLE);
            listView.setVisibility(View.INVISIBLE);
            SwapDetails swapDetails = swapBodyList.get(adapterView.getCount() - i - 1);
            fromLoginID = swapDetails.getSwapperLoginID();
            fromImageUrl = swapDetails.getSwapperImageUrl();
            fromName = swapDetails.getSwapperName();
            fromPhone = swapDetails.getSwapperPhone();
            fromEmail = swapDetails.getSwapperEmail();
            fromCompanyBranch = swapDetails.getSwapperCompanyBranch();
            fromAccount = swapDetails.getSwapperAccount();
            fromShiftDate = swapDetails.getSwapShiftDate();
            fromShiftDay = swapDetails.getSwapperShiftDay();
            fromShiftTime = swapDetails.getSwapperShiftTime();
            fromPreferredShift = swapDetails.getSwapperPreferredShift();
            String child = fromID + fromShiftDay + fromShiftTime + fromPreferredShift + toID + toShiftDay + toShiftTime + toPreferredShift;
            shiftSwapRequestsDb = FirebaseDatabase.getInstance().getReference().child("Swap Requests").child("Shift Request")
                    .child(child);
            swapRequestShift = new SwapRequestShift(toID,
                    toLoginID,
                    toImageUrl,
                    toName,
                    toPhone,
                    toEmail,
                    toCompanyBranch,
                    toAccount,
                    toShiftDate,
                    toShiftDay,
                    toShiftTime,
                    toPreferredShift,
                    fromID,
                    fromLoginID,
                    fromImageUrl,
                    fromName,
                    fromPhone,
                    fromEmail,
                    fromCompanyBranch,
                    fromAccount,
                    fromShiftDate,
                    fromShiftDay,
                    fromShiftTime,
                    fromPreferredShift,
                    -1,
                    -1);
            shiftSwapRequestsDb.setValue(swapRequestShift)
                    .addOnSuccessListener(new OnSuccessListener<Void>() {
                        @Override
                        public void onSuccess(Void aVoid) {
                            //set the request message
                            requestMessage = userName + "" + " wants to swap with your shift";

                            Map<String, Object> notificationMessage = new HashMap<>();
                            notificationMessage.put("message", requestMessage);
                            notificationMessage.put("from", currentUserId);

                            notificationDB.child(swapperID).push()
                                    .setValue(notificationMessage).addOnCompleteListener(new OnCompleteListener<Void>() {
                                @Override
                                public void onComplete(@NonNull Task<Void> task) {
                                    if (task.isSuccessful()) {
                                        progressBar.setVisibility(View.INVISIBLE);
                                    }
                                }
                            }).addOnFailureListener(new OnFailureListener() {
                                @Override
                                public void onFailure(@NonNull Exception e) {
                                    Toast.makeText(ProfileActivityShift.this, "Something went wrong", Toast.LENGTH_LONG).show();
                                    Log.e(LOG_TAG, "Failed to insert row for " + currentUserId);
                                }
                            });
                            Toast.makeText(ProfileActivityShift.this, "Notification sent", Toast.LENGTH_LONG).show();
                            progressBar_ShiftProfileChooseDialog.setVisibility(View.INVISIBLE);
                            listView.setVisibility(View.VISIBLE);
                            chooseShiftProfileDialog.dismiss();
                            shiftProfileDialog.dismiss();
                            progressBar.setVisibility(View.INVISIBLE);
                            textSentOrAcceptedRequest.setVisibility(View.VISIBLE);
                        }
                    }).addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    Toast.makeText(ProfileActivityShift.this, e.getMessage(), Toast.LENGTH_LONG).show();
                    progressBar_ShiftProfileChooseDialog.setVisibility(View.INVISIBLE);
                    listView.setVisibility(View.VISIBLE);
                    chooseShiftProfileDialog.dismiss();
                    shiftProfileDialog.dismiss();
                    progressBar.setVisibility(View.INVISIBLE);
                    buttonSwapRequest.setVisibility(View.VISIBLE);
                }
            });
        }
    });

}

推荐答案

问题已解决. 原因是if语句在找到用户ID的情况下将整个数据添加到swapdetails对象中,然后找到了另一个ID并再次添加了所有这些信息,等等.

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.

所以我只是在if语句中创建了bloolean,并使其在找到交换器ID的情况下成立,然后在addChildEventListene范围之外使用适配器.

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

shiftProfileAdapter.add(swapDetails);

这篇关于如何从实时数据库中删除填充有对象的Listview中的重复项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆