我发现一个问题是,当我转回片段时,它将显示重复的数据。
我尝试清除arrayList数据来解决它。
但是我想知道更聪明的方法。
是否有可能避免重复数据出现此问题?
适配器类
myHolder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View v) {
CharSequence options[] = new CharSequence[]{"Accept Request", "Cancel Request"};
final AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setItems(options, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (which == 0){
acceptRequest(requestList.get(i).getId(),myHolder);
}
else if (which == 1){
cancelRequest(requestList.get(i).getId(),myHolder);
}
}
});
builder.show();
}
});
}
private void cancelRequest(final String uid, final MyHolder myHolder) {
friendReqDbRef
.child(current_uid)
.child(uid)
.removeValue()
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
friendReqDbRef
.child(uid)
.child(current_uid)
.removeValue()
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
requestList.remove(myHolder.getAdapterPosition());
notifyItemRemoved(myHolder.getAdapterPosition());
notifyItemRangeChanged(myHolder.getAdapterPosition(), requestList.size());
}
});
}
});
}