HemalHerath
HemalHerath

Reputation: 1056

How can I retrieve all users from firebase to a fragment?

I have created a Recycler view in my fragment_add_friends.xml like below

<android.support.v7.widget.RecyclerView
android:id="@+id/all_user_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical" />

and I created all_user_display_layout.xml file for display each user like below

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <de.hdodenhof.circleimageview.CircleImageView
            android:id="@+id/all_user_image">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:id="@+id/all_user_name"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="full name"/>

            <TextView
                android:id="@+id/all_user_status"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="user status"/>

        </LinearLayout>

    </LinearLayout>

</LinearLayout>

In my AddFriendsFragment.java I got the recycler view like below in my on create view method

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_add_friends, container, false);

        all_user_list = (RecyclerView) v.findViewById(R.id.all_user_list);
        all_user_list.setHasFixedSize(true);
        all_user_list.setLayoutManager(new LinearLayoutManager(getActivity()));

        return v;
    }

and I have created a helper class that have Strings name, status, image and there getters setters and constructor and empty construcor because I have to retrieve those data and xml file those things in is all_user_display_layout.xml

and I got the database reference like below

databaseReference = FirebaseDatabase.getInstance().getReference().child("users");

and I create the onViewCreated method like below

@Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);


    }

after that what are the things that I have to do to get data from fireabse to recycler view

please help me im new to these stuff

database structure

Upvotes: 0

Views: 2567

Answers (2)

kunwar97
kunwar97

Reputation: 825

Remove these lines from onCreateView

 all_user_list = (RecyclerView) v.findViewById(R.id.all_user_list);
 all_user_list.setHasFixedSize(true);
 all_user_list.setLayoutManager(new LinearLayoutManager(getActivity()));

Add the following code in onViewCreated

@Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

    FirebaseRecyclerViewAdapter<<Your-Helper-Class>, ViewHolder> adapter;
     ref = new Firebase("https://<yourapp>.firebaseio.com");

     RecyclerView recycler = (RecyclerView) findViewById(R.id.all_user_list);
     recycler.setHasFixedSize(true);
     recycler.setLayoutManager(new LinearLayoutManager(getActivity()));

     adapter = new FirebaseRecyclerViewAdapter<<Your-Helper-Class>, ViewHolder>(<Your-Helper-Class>.class, android.R.layout.all_user_display_layout.xml, ViewHolder.class, mRef) {
         public void populateViewHolder(ViewHolder viewHolder, <Your-Helper-Class> helper) {
            Picasso.with(context).load(helper.getImage()).into(viewHolder.image);
             viewHolder.name.setText(helper.getName());
             viewHolder.status.setText(helper.getStatus());
         }
     };
     recycler.setAdapter(mAdapter);
    }



    private static class ViewHolder extends RecyclerView.ViewHolder {
        CircleImageView image;
         TextView name;
         TextView status;

         public ChatMessageViewHolder(View itemView) {
             super(itemView);
             image = (TextView)itemView.findViewById(android.R.id.all_user_image);
             name = (TextView)itemView.findViewById(android.R.id.all_user_name);
             status = (TextView) itemView.findViewById(android.R.id.all_user_status);
         }
     }

Also use Picasso for loading image in imageview. Add these lines in your build.gradle.

compile 'com.squareup.picasso:picasso:2.5.2'

Upvotes: 1

Alex Mamo
Alex Mamo

Reputation: 138824

Assuming that users node is a direct child of your Firebase database root, to get the name, image and status, please use the following code:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference usersRef = rootRef.child("users");
ValueEventListener eventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        List <String> list = new ArrayList<>();
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            String name = ds.child("name").getValue(String.class);
            String image = ds.child("image").getValue(String.class);
            String status = ds.child("status").getValue(String.class);
            list.add(name + " / " + image + " / " + status);
            Log.d("TAG", name + " / " + image + " / " + status);
        }
        Log.d("TAG", list);
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
usersRef.addListenerForSingleValueEvent(eventListener);

Your output will be:

Hemal / https... / Busy
Jake Stewart / https... / Busy

Upvotes: 2

Related Questions