valueEventListener用于从firebase数据库检索数据的更好的替代方法是什么? [英] What is a better alternative to valueEventListener for retrieving data from firebase database?

查看:127
本文介绍了valueEventListener用于从firebase数据库检索数据的更好的替代方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Firebase数据库就是这样 -

My Firebase database is like that -

           users - 
                     first user ID 
                                  - name - "abc"
                                  - image - "url"
                                  - one_word - "abc"

           following -
                      first user ID -
                                     second User ID - "0"

以下节点显示第一位用户正在关注第二位用户。

Following node shows that First user is following second user.

这是我的代码 -

     @Override
protected void onStart() {
    super.onStart();
    imageView.setVisibility(View.GONE);

    FirebaseRecyclerAdapter<followers_following_class,following_Adapter>firebaseRecyclerAdapter =
            new FirebaseRecyclerAdapter<followers_following_class, following_Adapter>
                    (
                            followers_following_class.class,
                            R.layout.find_friend_card,
                            following_Adapter.class,
                            databaseReference
                    ) {
                @Override
                protected void populateViewHolder(final following_Adapter viewHolder, final followers_following_class model, int position) {
                    final String user_id = getRef(position).getKey();



                    users.child(user_id).addValueEventListener(new ValueEventListener() {
                        @Override
                        public void onDataChange(DataSnapshot dataSnapshot) {
                            final  String name = dataSnapshot.child("name").getValue().toString();
                            final String image = dataSnapshot.child("image").getValue().toString();
                            final String line = dataSnapshot.child("line").getValue().toString();
                            final String wins = dataSnapshot.child("one_word").getValue().toString();

                            viewHolder.setName(name);
                            viewHolder.setImage(following.this,image);
                            viewHolder.setLine(line);
                            viewHolder.setOne_word(wins);

                            if(getItemCount() == 0){
                                imageView.setVisibility(View.VISIBLE);
                            }

                            viewHolder.vieww.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    if(!user_id.equals(my_id)){
                                        Intent intent = new Intent(following.this,Friend_profile_view.class);
                                        intent.putExtra("user_id",user_id);
                                        intent.putExtra("image",image);
                                        intent.putExtra("one_word",wins);
                                        intent.putExtra("name",name);
                                        startActivity(intent);
                                    }
                                }
                            });

                        }

                        @Override
                        public void onCancelled(DatabaseError databaseError) {

                        }
                    });
                }
            };
    list.setAdapter(firebaseRecyclerAdapter);
}

public static class following_Adapter extends RecyclerView.ViewHolder {
    View vieww;
    public following_Adapter(View itemView) {
        super(itemView);
        this.vieww = itemView;
    }

    public void setImage( final following following, final String image) {
        final CircleImageView circleImageView = (CircleImageView)vieww.findViewById(R.id.find_friend_profile_image_card);
        if(!image.equals("default_image")) {
            Picasso.with(following).load(image).networkPolicy(NetworkPolicy.OFFLINE).into(circleImageView, new Callback() {
                @Override
                public void onSuccess() {
                }

                @Override
                public void onError() {
                    Picasso.with(following).load(image).into(circleImageView);
                }
            });
        }
    }

    public void setName(String name) {
        TextView textView = (TextView)vieww.findViewById(R.id.find_friends_name_card);
        textView.setText(name);
    }

    public void setLine(String line) {
        ImageView imageView = (ImageView)vieww.findViewById(R.id.online_or_not);
        if(line.equals("offline")){
            imageView.setVisibility(View.INVISIBLE);
        }
    }

    public void setOne_word(String wins) {
        TextView textView = (TextView)vieww.findViewById(R.id.user_level);
        textView.setText(wins);
    }
}

我有什么方法可以申请firebase回收适配器对于一个节点但是使用相同的密钥从另一个节点检索数据而不使用addValueEventListener?

Is there any way where i can apply firebase recycler adapter for one node but retrieve data form another node with same key without using addValueEventListener ?


  • 我的大多数应用程序在所有活动中都使用firebase recyclerview所以,当我观察我的Android分析器时,我的RAM使用量增加,而在活动之间切换我也使用了finish();在onDistroy方法中结束了addValuelistener但它仍然无法正常工作。

推荐答案

有3个 eventListeners 您可以根据需要使用,即 valueEventListener childEventListener singleValueEventListener

There are 3 eventListeners that you can use according to your needs, namely valueEventListener, childEventListener and singleValueEventListener.

这是一个很好的解读, Firebase文档

This will be a good read for this, Firebase Docs.

使用列表时,您的应用程序应该监听子项事件而不是用于单个对象的值事件。

When working with lists, your application should listen for child events rather than the value events used for single objects.

触发子事件以响应来自诸如新操作的操作的节点子节点发生的特定操作通过 push()方法添加子项或通过 updateChildren()方法更新子项。这些中的每一个都可用于侦听对数据库中特定节点的更改。

Child events are triggered in response to specific operations that happen to the children of a node from an operation such as a new child added through the push() method or a child being updated through the updateChildren() method. Each of these together can be useful for listening to changes to a specific node in a database.

在代码中, childEventListener 看起来像这样:

In code, childEventListener looks like this:

ChildEventListener childEventListener = new ChildEventListener() {
    @Override
    public void onChildAdded(DataSnapshot dataSnapshot, String previousChildName) {
        Log.d(TAG, "onChildAdded:" + dataSnapshot.getKey());

        // A new comment has been added, add it to the displayed list
        Comment comment = dataSnapshot.getValue(Comment.class);

        // ...
    }

    @Override
    public void onChildChanged(DataSnapshot dataSnapshot, String previousChildName) {
        Log.d(TAG, "onChildChanged:" + dataSnapshot.getKey());

        // A comment has changed, use the key to determine if we are displaying this
        // comment and if so displayed the changed comment.
        Comment newComment = dataSnapshot.getValue(Comment.class);
        String commentKey = dataSnapshot.getKey();

        // ...
    }

    @Override
    public void onChildRemoved(DataSnapshot dataSnapshot) {
        Log.d(TAG, "onChildRemoved:" + dataSnapshot.getKey());

        // A comment has changed, use the key to determine if we are displaying this
        // comment and if so remove it.
        String commentKey = dataSnapshot.getKey();

        // ...
    }

    @Override
    public void onChildMoved(DataSnapshot dataSnapshot, String previousChildName) {
        Log.d(TAG, "onChildMoved:" + dataSnapshot.getKey());

        // A comment has changed position, use the key to determine if we are
        // displaying this comment and if so move it.
        Comment movedComment = dataSnapshot.getValue(Comment.class);
        String commentKey = dataSnapshot.getKey();

        // ...
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
        Log.w(TAG, "postComments:onCancelled", databaseError.toException());
        Toast.makeText(mContext, "Failed to load comments.",
                Toast.LENGTH_SHORT).show();
    }
};
ref.addChildEventListener(childEventListener);

此外,不使用 eventListeners检索数据是不可能的。如果你想同时听一个节点的孩子,那么 childEventListener 将是一个很好的工具。

Also, retrieving data without the use of eventListeners is not possible. And if you want to listen to children of your one node, simultaneously, then childEventListener will be a great tool.

这篇关于valueEventListener用于从firebase数据库检索数据的更好的替代方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆