![](/img/trans.png)
[英]how to delete the auto generated item from push method from Firebase Real-time database
[英]How to delete duplicated items in Listview filled with objects from real-time database
我正在基於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);
}
});
}
});
}
嘗試這個
@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);
}
}
}
問題解決了。 原因是如果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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.