Juy
Juy

Reputation: 17

Android - recyclerView return several times the same items

I'm trying to get firebase data, but the problem is that it returns the same data several times.

imageRef = database.getInstance().getReference("Users").child(userid);
imageRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {


            for (DataSnapshot mData : dataSnapshot.child("images").getChildren()) {

                for (DataSnapshot mDataTwo: dataSnapshot.child("locations").getChildren()) {

                    ItemData itemData = new ItemData();

                    String mImages = mData.getValue(String.class);
                    String mLocations = mDataTwo.getValue(String.class);

                    itemData.setImages(mImages);
                    itemData.setLocations(mLocations);

                    data.add(itemData);

                }

            }
            adapter.notifyDataSetChanged();

        }

UPDATE:

This is my database structure.

"Users" : {

"X7aVfZH5oZbIaeyLm9FAjOSr5Gd2" : {
  "images" : {
    "urlImages1" : "https://firebasestorage.googleapis.com/v0/b/anthiefy.appspot.com/o/X7aVfZH5oZbIaeyLm9FAjOSr5Gd2%2FImages%2FImg1.jpg?alt=media&token=e26fd6a2-2be7-4a01-920e-0aed1fe06436",
    "urlImages2" : "https://firebasestorage.googleapis.com/v0/b/anthiefy.appspot.com/o/X7aVfZH5oZbIaeyLm9FAjOSr5Gd2%2FImages%2FImg2.jpg?alt=media&token=40165918-3b46-4b22-b0ef-255965b7855d",
    "urlImages3" : "https://firebasestorage.googleapis.com/v0/b/anthiefy.appspot.com/o/X7aVfZH5oZbIaeyLm9FAjOSr5Gd2%2FImages%2FImg3.jpg?alt=media&token=cfd7d00d-9b1a-44fd-80d4-a2947f7de743",
    "urlImages4" : "https://firebasestorage.googleapis.com/v0/b/anthiefy.appspot.com/o/X7aVfZH5oZbIaeyLm9FAjOSr5Gd2%2FImages%2FImg4.jpg?alt=media&token=995419a7-70f7-4330-b046-d06a4d54453e",
    "urlImages6" : "https://firebasestorage.googleapis.com/v0/b/anthiefy.appspot.com/o/X7aVfZH5oZbIaeyLm9FAjOSr5Gd2%2FImages%2FImg6.jpg?alt=media&token=ee6f566a-a9e5-4e8f-9d53-64d3f3bc97a6",
    "urlImages7" : "https://firebasestorage.googleapis.com/v0/b/anthiefy.appspot.com/o/X7aVfZH5oZbIaeyLm9FAjOSr5Gd2%2FImages%2FImg7.jpg?alt=media&token=883a3321-fbba-49bd-aae3-14b005b4cc5b"
  },
  "locations" : {
    "locations1" : "14.2830817 -89.7263953",
    "locations2" : "14.2661424 -89.7220017",
    "locations3" : "14.2846352 -89.7251991",
    "locations4" : "14.2684987 -89.7266704",
    "locations5" : "14.2684987 -89.7266704",
    "locations6" : "14.2684987 -89.7266704",
    "locations7" : "14.2684987 -89.7266704"
  }

I have tried many ways, I have also sought the solution but without any fortune. If someone can help me, very grateful.

Upvotes: 0

Views: 113

Answers (2)

Nikul Vadher
Nikul Vadher

Reputation: 139

What you are doing is for every single Image you are iterating every location and creating object of that Item.

    List<String> images=new ArrayList<>();
    List<String> locations=new ArrayList<>();
    for (DataSnapshot mData : dataSnapshot.child("images").getChildren()) {
        String image = mData.getValue(String.class);
        images.add(image);


    }
    for (DataSnapshot mDataTwo: dataSnapshot.child("locations").getChildren()) {
        String location = mDataTwo.getValue(String.class);
        locations.add(location);
    }
    if (images.size()==locations.size()){
        for (int i=0;i<locations.size();i++){
            String image=images.get(i);
            String location=locations.get(i);
            ItemData itemData = new ItemData();
            itemData.setImages(image);
            itemData.setLocations(location);
            data.add(itemData);
        }
        adapter.notifyDataSetChanged();
    }

Try out this code. It'll help you.

Upvotes: 0

Urvish rana
Urvish rana

Reputation: 638

you need to clear the data object before it control goes into the loop. what happening in your app is when anything changes on firebase database it gives all the present data to mobile so everytime you change anything on the cloud the redundant data also comes into the DataSnapShot object so you need to clear the ArrayList or list that you've taken as 'data'.

Upvotes: 1

Related Questions