我正在根据从数据库中获取数据,userId然后将数据插入listview并通过对话框显示。
userId
listview
我一直在等待的行为是,我将获得用户创建的所有交换并将其插入列表中,以便他可以选择其中之一。但是,仅当他在数据库中只有一个交换时(如通常显示的那样)时,该代码才能正常工作。但是,如果他有两次交换,则列表内的交换将被乘以2。
如果是3,则数据将重复3次,依此类推。我不知道我的代码在这里的流程是什么,希望有人可以帮助我解决这个问题。如何清除列表中的重复项?
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,然后再次添加了所有其他数据,依此类推。
因此,仅在if语句中创建了bloolean,并在找到交换器ID的情况下将其设为true,然后在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);