简体   繁体   English

如何在Android RecyclerView中显示Firebase Firestore用户?

[英]How to display Firebase Firestore users in Android RecyclerView?

I am working on Cloud Firestore. 我正在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 . 我的Firestore数据库文档名称为users ,我想在Android RecyclerView中显示所有用户,我的活动名称为Alluser ,我正在使用此.XLM文件activity_alluser.xml

I am confused about what to pass parameter inside FirebaseRecyclerAdapter; 我对在FirebaseRecyclerAdapter内部传递参数感到困惑; here is my Java as well as my XML code. 这是我的Java以及XML代码。

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: XML代码:

<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 . 为了使FirebaseRecyclerAdapter正常工作,您需要将DatabaseReference作为最后一个参数传递,如官方文档中所示

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. Beeing一个抽象类,您还可以看一下FirebaseRecyclerAdapter类,以更清楚地了解应重写的方法。

Talking about Cloud Firestore , I recomand you using FirestoreRecyclerAdapter instead of FirebaseRecyclerAdapter . 在谈论Cloud Firestore ,我建议您使用FirestoreRecyclerAdapter而不是FirebaseRecyclerAdapter To use a this adapter, you'll also need a FirestoreRecyclerOptions in order to make it work. 要使用此适配器,您还需要一个FirestoreRecyclerOptions才能使其工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Android 在 RecyclerView 中显示来自 Firestore 的数据? - How to display data from Firestore in a RecyclerView with Android? 如何使用 Android 在 RecyclerView 中显示来自 Firestore 的文档及其子集合? - How to display a document with its subcollections from Firestore in a RecyclerView with Android? android - 如何在 RecyclerView 中显示来自 Cloud FireStore 的 object 数组 - android - how to display the array of object from Cloud FireStore in the RecyclerView Android Recyclerview 刷新问题(Firebase Firestore) - Android Recyclerview Refresh Problem (Firebase Firestore) 尝试在回收站视图中显示用户信息 - Firebase - Trying to display users information in a recyclerview - Firebase 如何将 integer 从 firebase firestore 显示到 Android Studio 的 TextView? - How to display integer from firebase firestore to Android Studio's TextView? 如何在从 firebase firestore 获取数据时在 recyclerview Android 上进行自动刷新 - How to make auto refreshing on recyclerview Android while getting data from firebase firestore 如何使用 java 在 android 工作室应用程序中由多个注册用户访问 Firebase Firestore 集合? - How to access Firebase Firestore collection by more than one registered users in android studio app using java? Android Firestore 子集合到 RecyclerView - Android Firestore subcollections into RecyclerView 如何将子集合中的所有文档获取到 Firebase Firestore 中的 RecyclerView - How to get all documents in sub-collections to RecyclerView in Firebase Firestore
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM