Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
453 views
in Technique[技术] by (71.8m points)

java - What is a better alternative to valueEventListener for retrieving data from firebase database?

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.
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...