简体   繁体   English

我如何不重复地从firebase数据库中获取数据

[英]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.

相关问题 如何从 Firebase Firestore 获取 ReactJs 的数据? - How Can I get data with ReactJs from Firebase Firestore? Firebase Realtime Database 数据检索的进度可以获取吗? - Can I get the progress of a Firebase Realtime Database data retrieval? 我无法从 expo 中的数据库 (firebase) 获取信息 - I can't get information from the database (firebase) in expo 如何从 Firebase 实时数据库中获取数据到 Flutter 的列表中? - How to get data from Firebase Realtime Database into list in Flutter? 如何从firebase实时数据库中获取int型数据 - How to get int type data from firebase real time database 如何从 Firebase 数据库调用帖子 ID - How can I call a post ID from a Firebase Database 如何从 Cloud Functions 访问和操作 Firebase 数据库? - How can I reach and manipulate Firebase Database from Cloud Functions? 我在数组中遇到问题我从 firebase (firestore) 获取数据,我想显示数据数组我该怎么做? - I face a problem in the array I get the data from firebase (firestore) and I want to show array of data how can i do? 如何使用 (.get) 从 firebase 读取数据(一次读取) - how can i read data from firebase (one time read ) using (.get) 如何在 Firebase (Kotlin) 中获取特定数据 - How can I get a specific data in Firebase (Kotlin)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM