简体   繁体   English

我正在从 firebase Realtime Db 获取数据,并在 recyclerview 问题上获取数据是 Collection.shuffling 在 android studio 中重复相同的项目

[英]I am getting data from firebase Realtime Db and fetching on recyclerview issue is Collection.shuffling is repeating same item in android studio

Here is my Code.这是我的代码。 Also, I am using auto-clicking on index 0, How do I Stop Repeating items when shuffling?另外,我正在使用自动单击索引 0,如何在洗牌时停止重复项目?

    if (ref != null) {
        ref.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                if (dataSnapshot.exists()) {
                    list = new ArrayList<>();
                    String state;
                    String UserEmail;
                    for (DataSnapshot ds : dataSnapshot.getChildren()) {
                        Deal value = ds.getValue(Deal.class);
                        value.setKey(ds.getKey());
                        state = "" + ds.child("VideoStatus").getValue();
                        if (state.equals("incomplete"))
                            list.add(value);
                        Collections.shuffle(list);

                    }
                    AdapterClassssautoclicker adapterClassssautoclicker = new AdapterClassssautoclicker(list);
                    recyclerView.setAdapter(adapterClassssautoclicker);

// recyclerView.swapAdapter(adapterClassssautoclicker); // recyclerView.swapAdapter(adapterClassssautoclicker);

                }


                int pos = 0;
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        if (recyclerView.findViewHolderForAdapterPosition(pos) != null)
                            Objects.requireNonNull(recyclerView.findViewHolderForAdapterPosition(pos)).itemView.performClick();
                    }
                }, 1);

            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
                Toast.makeText(getApplicationContext(), databaseError.getMessage(), Toast.LENGTH_SHORT).show();

            }
        });

    }


}

According to your last comment:根据你最后的评论:

Code is repeating Items I want to remove repetition while shuffling代码正在重复我想在洗牌时删除重复项

To solve this issue, you have to get the following line of code:要解决此问题,您必须获得以下代码行:

Collections.shuffle(list);

Out of the for loop as you see in the following line of code:正如您在以下代码行中看到的那样,退出 for 循环:

for (DataSnapshot ds : dataSnapshot.getChildren()) {
    Deal value = ds.getValue(Deal.class);
    value.setKey(ds.getKey());
    state = "" + ds.child("VideoStatus").getValue();
    if (state.equals("incomplete"))
        list.add(value);
}
Collections.shuffle(list);

In this way, you are shuffling the elements only once, and not at every iteration.通过这种方式,您只对元素进行了一次混洗,而不是每次迭代都进行混洗。 In this way you can avoid duplicate elements.通过这种方式,您可以避免重复元素。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法在 Android Studio 中从 Firebase 实时数据库检索数据 - Can't retrieve data from Firebase Realtime db in Android Studio 从Firebase实时数据库,Android Studio,Java获取数据 - Getting Data from Firebase realtime database, Android Studio, Java 当用户滚动到RecyclerView的底部时,使用ScrollListener从Firebase Realtime DB中提取数据 - Issue using ScrollListener to pull data from Firebase Realtime DB when user scrolls to the bottom of a RecyclerView 我正在尝试将 firebase 实时数据库中的数据检索到我的 recyclerview 中,但数据显示 null - I am trying to retrieve data from firebase realtime database into my recyclerview but data is showing null 使用 android studio 中的 recyclerview 适配器从 firebase 获取特定密钥 - Fetching specific key from firebase using recyclerview adapter in android studio 从android studio中的firebase获取特定数据 - Fetching specific data from firebase in android studio 将 RecylerView 项目数据发送到另一个片段。 我正在从 Firebase 实时数据库获取数据 - Send RecylerView Items Data To Another Fragment. I Am Getting Data From Firebase RealTime Database 我正在尝试从 android studio 的 recyclerview 中的 firestore 中检索数据,但得到空白页面并且没有错误我尝试了很多方法但没有工作 - I am trying to retrieved data from firestore in recyclerview in android studio but getting blank page and no error i have tried many way but not work 从 Android Studio 中的实时 firebase 数据库中检索数据 - Retrieve data from realtime firebase database in Android Studio RecyclerView 未从 firebase 数据库中获取数据 - RecyclerView not fetching data from firebase database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM