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