[英]how I can get data from the firebase database without repeat
I am developing a android app work with the firebase database, when the user add item to the database the app display it at the Realtime and that what I want, but the problem it is that same item show more than one time in the RecyclerView this is the code that I try我正在开发一个 android 应用程序与 firebase 数据库一起工作,当用户将项目添加到数据库时,应用程序会实时显示它,这就是我想要的,但问题是同一个项目在 RecyclerView 中显示多次是我尝试的代码
data = new ArrayList<>();
DatabaseReference myRef = FirebaseDatabase.getInstance().getReference().child("workers");
myRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
if (snapshot.exists()) {
e_iv.setVisibility(View.GONE);
e_tv.setVisibility(View.GONE);
rv_all.setVisibility(View.VISIBLE);
for (DataSnapshot ds : snapshot.getChildren()) {
for (DataSnapshot d : ds.getChildren()) {
data.add(d.getValue(work.class));
}
}
mAdapter = new aAdapter(data);
rv_all.setAdapter(allGamesAdapter);
mAdapter.notifyDataSetChanged();
}
But it does not solve it,and a side note when I go out from the app and enter again it is works perfectly and show the items without repeat anyone,How can I solve that?但它并没有解决它,当我从应用程序中退出 go 并再次输入时,它可以完美地工作并且显示项目而不重复任何人,我该如何解决这个问题?
What happening is the ArrayList is getting data added every time listener returns the data.每次侦听器返回数据时,ArrayList 都会添加数据。 To remove duplicacy just clear the arrayllist inside the onDataChange() function要删除重复项,只需清除onDataChange() function 中的 arrayllist
data = new ArrayList<>();
DatabaseReference myRef =
FirebaseDatabase.getInstance().getReference().child("workers");
myRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
if (snapshot.exists()) {
data.clear();
e_iv.setVisibility(View.GONE);
e_tv.setVisibility(View.GONE);
rv_all.setVisibility(View.VISIBLE);
for (DataSnapshot ds : snapshot.getChildren()) {
for (DataSnapshot d : ds.getChildren()) {
data.add(d.getValue(work.class));
}
}
mAdapter = new aAdapter(data);
rv_all.setAdapter(allGamesAdapter);
mAdapter.notifyDataSetChanged();
}
Hope your bug is resolved.希望你的错误得到解决。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.