为什么我从firebase数据库中获取重复数据?

时间:2017-05-03 17:26:14

标签: android firebase firebase-realtime-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);
    }
}

这是我的数据库结构

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

我得到重复的数组并且无法理解原因 请帮我。 我也尝试删除一个引用并拨打电话,但这样我就无法访问JSON的子代。

2 个答案:

答案 0 :(得分:1)

这是因为您for loop而发生的。每次循环时都会创建一个新的databaseReference。从您的循环中取出databaseReference,它将解决您的问题或尝试更改从Firebase数据库获取数据的逻辑。

希望它有所帮助。

答案 1 :(得分:0)

公共类帖子扩展了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) {

        }
    });
}

}

大家好,

我通过对数据库的一次引用解决了我的问题,而不是像之前使用addChildEventListener那样进行了两次调用。

由于