1

This is my randomly retrieving data codes:

            mDataSelect.addValueEventListener(new ValueEventListener() {

                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    List<String> lst = new ArrayList<String>();

                    for (DataSnapshot ds : dataSnapshot.getChildren()) {

                        Collections.shuffle(lst);
                        lst.add(String.valueOf(ds.getKey()));

                        randomGenerator = new Random();
                        int index = randomGenerator.nextInt(lst.size());
                        String item = lst.get(index);
                        idofcaps.setText(item);

                        final DatabaseReference capsSelect = mDataSelect.child(item);

                        capsSelect.addValueEventListener(new ValueEventListener() {
                            @Override
                            public void onDataChange(DataSnapshot dss) {

                                String post_Image = (String) dss.child("Image").getValue();
                                Picasso.with(StartCapsActivity.this).load(post_Image).into(caps);
                                String post_name = (String) dss.child("Translation").getValue();
                                translation.setText(post_name);


                            }

                            @Override
                            public void onCancelled(DatabaseError databaseError) {

                            }
                        });

                    }}

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });

        }
    });

I have a next button in my screen which has the same code for retrieve another image from database. However, I don't want to retrieve same image again. How can I prevent this redundancy?

AL.
  • 36,815
  • 10
  • 142
  • 281

1 Answers1

3

Create an arraylist then add the retrieved key. Then remove possible duplicate keys. How do I remove repeated elements from ArrayList?

Community
  • 1
  • 1
elbert rivas
  • 1,464
  • 1
  • 17
  • 15
  • But I'm displaying them one by one and none of them has the same key. I tried some of the answer. It prevent to display same images back to back. However, when I click the next button third time the first image can display. I have 20 images in my database. I want to show them one by one but there is no priority like some of them should be previous. – BüsraNur Akgül Jan 22 '17 at 18:21