Android Firebase查询未获取更新的数据

时间:2017-07-11 00:40:31

标签: android firebase firebase-realtime-database android-recyclerview recycler-adapter

我有一个自定义对象的RecyclerView,用于从Firebase提取数据。在每个项目中,都有一个" likeButton"增加某个评论的喜欢。由于RecyclerViews没有onItemClickListener方法,我已在适配器类的内部ViewHolder类中声明了这一点:

public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
        private TextView username, chirp, time, likeCount;
        private ImageButton likeButton;

        public ViewHolder(View itemView) {
            super(itemView);
            username = (TextView) itemView.findViewById(R.id.home_feed_item_username);
            chirp = (TextView) itemView.findViewById(R.id.home_feed_item_chirp);
            time = (TextView) itemView.findViewById(R.id.home_feed_item_time);
            likeCount = (TextView) itemView.findViewById(R.id.home_feed_item_like_count);
            likeButton = (ImageButton) itemView.findViewById(R.id.home_feed_item_like_button);

            //set click listener for likeButton
            likeButton.setOnClickListener(this);

            //set click listener for whole view, which will take them to the details activity
            itemView.setOnClickListener(this);
        }

        //define click listener for likeButton
        @Override
        public void onClick(View v) {
            if(v.getId() == likeButton.getId()){
                //access Firebase
                final DatabaseReference reference = FirebaseDatabase.getInstance().getReference().child("chirps");

                //query for this specific chirp
                Query query = reference.orderByChild("chirp").equalTo(chirp.getText().toString());
                query.addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        if(dataSnapshot.exists()){
                            //convert the dataSnapshot
                            HomeFeedItem queryResult = dataSnapshot.getValue(HomeFeedItem.class);
                            //here, it keeps printing out 0
                            System.out.println(queryResult.getLikeCount());

                            //increment it
                            int likeCountUpdated = queryResult.getLikeCount() + 1;

                            //update the likeCount textview
                            likeCount.setText(String.valueOf(likeCountUpdated));

                            //update the likeCount in the database
                            for (DataSnapshot child: dataSnapshot.getChildren()) {
                                reference.child(child.getKey()).child("likeCount").setValue(likeCountUpdated);
                            }
                        }
                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {

                    }
                });

                Toast.makeText(v.getContext(), "Ahh, you liked "+username.getText().toString()+"'s chirp!", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(v.getContext(), "Going to chirp activity...", Toast.LENGTH_SHORT).show();
            }
        }
    }

因此,我们的想法是,无论何时用户点击likeButton,它都会增加类似的计数并更新Firebase。但是,这仅在我第一次单击likeButton时起作用 - likeCount textView和Firebase都会增加1,但只是第一次,换句话说。在我的SOUT语句中,对于后续点击,它一直说likeCount是0,所以它看起来像是查询HomeFeedItem的第一个实例,而不是更新的Firebase对象。关于如何在每次单击时查询更新的Firebase对象的想法,而不仅仅是获取原始对象?谢谢!

1 个答案:

答案 0 :(得分:0)

对Firebase数据库执行查询时,可能会有多个结果。因此快照包含这些结果的列表。即使只有一个结果,快照也会包含一个结果的列表。

因此,您的代码需要通过循环快照的子代来处理该列表:

Query query = reference.orderByChild("chirp").equalTo(chirp.getText().toString());
query.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for (DataSnapshot childSnapshot: dataSnapshot.getChildren()) {
            //convert the dataSnapshot
            HomeFeedItem queryResult = childSnapshot.getValue(HomeFeedItem.class);
            //here, it keeps printing out 0
            System.out.println(queryResult.getLikeCount());

            //increment it
            int likeCountUpdated = queryResult.getLikeCount() + 1;

            //update the likeCount textview
            likeCount.setText(String.valueOf(likeCountUpdated));
        }
    }
    ...