简体   繁体   中英

How to display Firebase Firestore users in Android RecyclerView?

I am working on Cloud Firestore. My Firestore database document name is users and I want to show all users in Android RecyclerView and my activity name Alluser and I'm using this .XLM file activity_alluser.xml .

I am confused about what to pass parameter inside FirebaseRecyclerAdapter; here is my Java as well as my XML code.

public class Alluser extends AppCompatActivity
{
    private final static String TAG = Alluser.class.getSimpleName();
    private EditText mSearchField;
    private ImageButton mSearchBtn;
    private RecyclerView mUsersList;

    private FirebaseAuth mAuth;
    private FirebaseUser mCurrentUser;
    private DocumentReference mUser; 

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_alluser);

        mUsersList = (RecyclerView)findViewById(R.id.result_list);
        mSearchField = (EditText) findViewById(R.id.search_edittext);
        mUsersList.setHasFixedSize(true);
        mUsersList.setLayoutManager(new LinearLayoutManager(this));


        mAuth = FirebaseAuth.getInstance();
        //String current_user_id = mCurrentUser.getUid();
        String current_user_id = mAuth.getCurrentUser().getUid();
        mCurrentUser = FirebaseAuth.getInstance().getCurrentUser();


        mUser = FirebaseFirestore.getInstance().collection("users").document(current_user_id);

        mSearchBtn.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                firebaseUserSearch();
            }
        });
    }

    private void firebaseUserSearch()
    {
        FirebaseRecyclerAdapter<Users,UserviewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Users, UserviewHolder>
                (Users.class,
                        R.layout.single_user_list_layout,
                        UserviewHolder.class,
                        //i am confused what to pass here)
        {
            @Override
            protected void onBindViewHolder(@NonNull UserviewHolder holder, int position, @NonNull Users model) {

            }

            @Override
            public UserviewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                return null;
            }
        };
        mUsersList.setAdapter(firebaseRecyclerAdapter);
    }

    public class UserviewHolder extends RecyclerView.ViewHolder
    {
        View mView;

        public UserviewHolder(View itemView)
        {
            super(itemView);
            mView = itemView;
        }
        public void setDetails(String username,String useremail,String userimage)
        {
            TextView user_name= (TextView)mView.findViewById(R.id.single_user_name);
            TextView user_email =(TextView)mView.findViewById(R.id.single_user_email);
            CircleImageView user_image = (CircleImageView)mView.findViewById(R.id.circleImageView);

            user_name.setText(username);
            user_email.setText(useremail);
        }
    }
}

XML code:

<android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="15dp"
        android:background="@drawable/fragment_background">


        <TextView
            android:id="@+id/textView3"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:text="Search All User"
            android:paddingLeft="20sp"
            android:textColor="#000000"
            android:textSize="24sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintHorizontal_bias="0.057"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.031" />

        <EditText
            android:id="@+id/search_edittext"
            android:layout_width="250dp"
            android:layout_height="50dp"
            android:background="@drawable/search_layout"
            android:ems="10"
            android:hint="Enter Email to Search..."
            android:inputType="textEmailAddress"
            android:paddingLeft="20dp"
            android:paddingRight="15dp"
            android:textColor="#999999"
            android:textSize="16sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintHorizontal_bias="0.079"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.149" />

        <ImageButton
            android:id="@+id/search_button"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:background="@drawable/search_icon"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintHorizontal_bias="0.98"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.149" />

        <android.support.v7.widget.RecyclerView
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:id="@+id/result_list"
            android:layout_marginBottom="8dp"
            android:layout_marginEnd="7dp"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="7dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="150dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.625">

        </android.support.v7.widget.RecyclerView>

    </android.support.constraint.ConstraintLayout>

In order to make a FirebaseRecyclerAdapter work, you need to pass as the last argument a DatabaseReference , as seen in the official documentation .

So your code should look like this:

FirebaseRecyclerAdapter<Users,UserviewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Users, UserviewHolder>
            (Users.class,
             R.layout.single_user_list_layout,
             UserviewHolder.class,
             mUser)

Beeing an abstract class, you can also take a look a FirebaseRecyclerAdapter class, to see more clearly what method you should override.

Talking about Cloud Firestore , I recomand you using FirestoreRecyclerAdapter instead of FirebaseRecyclerAdapter . To use a this adapter, you'll also need a FirestoreRecyclerOptions in order to make it work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM