Shai Shamai
Shai Shamai

Reputation: 21

Why I'm getting duplicated data from firebase database?

public class Post {

    private String title;
    private String massage;

    public Post() {
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getMassage() {
        return massage;
    }

    public void setMassage(String massage) {
        this.massage = massage;
    }
}

public class Posts extends AppCompatActivity {

    private DatabaseReference databaseReference, barRoomsCoordinates;
    private FirebaseDatabase firebaseDatabase;
    private List<Post> postList;
    private List<String> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_posts);

        postList = new ArrayList<>();
        list = new ArrayList<>();

        showAllMyPosts();


    }

    private void getPosts(List<String> posts) {

        for (int i = 0; i < posts.size(); i++) {
            databaseReference = firebaseDatabase.getReference().child("Posts").child(posts.get(i));
            databaseReference.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    Post posts = dataSnapshot.getValue(Post.class);
                    Post post = new Post();

                    post.setTitle(posts.getTitle());
                    post.setMassage(posts.getMassage());
                    postList.add(post);



                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });
        }
    }

    private void showAllMyPosts() {
        FirebaseApp.initializeApp(Posts.this);

        Handler handler = new Handler();
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                barRoomsCoordinates = firebaseDatabase.getReference().child("Posts");
                barRoomsCoordinates.addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        Iterator iterator = dataSnapshot.getChildren().iterator();
                        while (iterator.hasNext()) {
                            list.add(((DataSnapshot) iterator.next()).getKey());
                        }
                        getPosts(list);
                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {

                    }
                });
            }
        };
        handler.post(runnable);
    }
}

This is my database structure

Posts
 Post1
 message1: 
"abc + something"
 title1: 
"abc"
 Post2
 message2: 
"def + something"
 title2: 
"def"
 Post3
 message3: 
"ghi + something"
 title3: 
"ghi"

I'm getting duplicated array and can't understand why please help me. I also tried delete one reference and make a call but in this way I can't access the children of the JSON.

Upvotes: 1

Views: 1471

Answers (2)

Shai Shamai
Shai Shamai

Reputation: 21

public class Posts extends AppCompatActivity {

private DatabaseReference databaseReference, barRoomsCoordinates;
private FirebaseDatabase firebaseDatabase;
private List<Post> postList;
private List<String> list;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_posts);

    postList = new ArrayList<>();
    list = new ArrayList<>();

    getPosts();


}

private void getPosts() {

    databaseReference = firebaseDatabase.getReference().child("Posts");
    databaseReference.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {

            Iterator iterator = dataSnapshot.getChildren().iterator();
            while (iterator.hasNext()) {
                Post post = new Post();
                post.setTitle((String) ((DataSnapshot)iterator.next()).getValue());
                post.setMassage((String) ((DataSnapshot)iterator.next()).getValue());

                postList.add(post);

            }

        }

        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {

        }

        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
}

}

Hello all,

I solved my problem with one reference to the database instead of two calls as i did before and with addChildEventListener.

Thanks

Upvotes: 0

Alex Mamo
Alex Mamo

Reputation: 138824

This is happening because of your for loop. You are creating a new databaseReference every time you loop. Take the databaseReference out of your loop and it will solve your problem or try to change the logic of getting the data from your Firebase database.

Hope it helps.

Upvotes: 1

Related Questions