我正在尝试检索根据其ID创建的用户数据。我创建了一个对话框,并在其中创建了一个列表视图,并为其设置了适配器以向他显示swaps(data)。该代码有效,但存在重复列表中数据的问题。因此,如果用户创建了两个拖车交换,那么他会在列表中看到他的拖车交换,并在此之后重复拖曳其他拖车,如果创建了五个,则该交换在列表中会重复五次。简单地说,它就等于存在的交换数量成倍增加。如果您创建了17个,则列表中的17个交换将重复17次。
我不知道是什么原因导致此错误,因为我什至将代码从使用 addChildEventListener 更改为 addListenerForSingleValueEvent 仍然存在。
这是我用来通过数据库节点获取数据的方法。
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);
}
});
}
});
}
我尝试使用addListenerForSingleValueEvent,但结果相同。
FirebaseDatabase.getInstance().getReference().child("swaps").child("shift_swaps")
.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
SwapDetails swapDetails = snapshot.getValue(SwapDetails.class);
if (swapDetails.getSwapperID().equals(fromID)) {
shiftProfileAdapter.add(swapDetails);
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});