2

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.

Here is my code -

     @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);
    }
}

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 ?

  • And also most of my app uses firebase recyclerview in all activities so when i observed my android profiler , my RAM usage is increasing while switching between activities i have also used finish(); ended the addValuelistener in onDistroy method but it is still not working.
PradyumanDixit
  • 2,372
  • 2
  • 12
  • 20
Jas world
  • 33
  • 7
  • Hey @Jasworld do mark the answer as correct by clicking the tick mark looking button next to the answer, this helps the future readers of the question and I'd also appreciate that. Cheers! :) – PradyumanDixit Nov 18 '18 at 03:40

1 Answers1

2

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

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.

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.

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);

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.

PradyumanDixit
  • 2,372
  • 2
  • 12
  • 20