从Firebase数据库检索数据的valueEventListener更好的替代方法是什么?

时间:2018-09-16 05:30:38

标签: java android firebase firebase-realtime-database

我的Firebase数据库就是这样-

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

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

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

这是我的代码-

     @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 Recycler适配器应用于一个节点,而无需使用addValueEventListener从具有相同键的另一节点检索数据吗?

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

1 个答案:

答案 0 :(得分:2)

您可以根据需要使用3个python font.py my-font.ttf | sh ,分别是eventListenersvalueEventListenerchildEventListener

Firebase Docs很好读。

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

响应于特定操作而触发了子事件,这些操作发生在某个节点的子对象上,例如通过singleValueEventListener方法添加的新子对象或通过push()方法进行更新的子对象之类的操作。所有这些都可以用于侦听数据库中特定节点的更改。

在代码中,updateChildren()如下所示:

childEventListener

同样,在不使用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将是一个很好的工具。