请让我知道为什么我的recyclerview确实没有在用户片段中显示用户列表。我正在关注此GitHub教程,但问题是他们使用了Firebase Realtime数据库,而我使用的是Firebase Firestore。我是android编程的初学者,现在我自己在you-tube上学习它,如果我的错误很愚蠢,我深表歉意。我要显示用户的数据库的屏幕快照为over here.
UserFragment.java
package com.example.authenticatorapp.Fragments;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.authenticatorapp.Adapters.UserAdapter;
import com.example.authenticatorapp.R;
import com.firebase.ui.firestore.FirestoreRecyclerOptions;
import com.example.authenticatorapp.User;
import com.google.firebase.firestore.CollectionReference;
import com.google.firebase.firestore.FirebaseFirestore;
import java.util.ArrayList;
import java.util.List;
public class UsersFragment extends Fragment {
private FirebaseFirestore db = FirebaseFirestore.getInstance();
private CollectionReference userRef = db.collection("users");
private UserAdapter userAdapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_users, container, false);
RecyclerView recyclerView1 = view.findViewById(R.id.recyclerview);
recyclerView1.setHasFixedSize(true);
recyclerView1.setLayoutManager(new LinearLayoutManager(getContext()));
readUsers();
recyclerView1.setAdapter(userAdapter);
return view;
}
private void readUsers(){
FirestoreRecyclerOptions<User> options = new FirestoreRecyclerOptions.Builder<User>()
.setQuery(userRef, User.class)
.build();
userAdapter = new UserAdapter(options);
}
}
fragment_users.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#660000"
tools:context=".Fragments.UsersFragment">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent">
</androidx.recyclerview.widget.RecyclerView>
</RelativeLayout>
User.java
package com.example.authenticatorapp;
public class User {
private String fName;
public User() {
}
public User(String fName) {
this.fName = fName;
}
public String getfName() {
return fName;
}
}
UserAdapter.java
package com.example.authenticatorapp.Adapters;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.example.authenticatorapp.R;
import com.example.authenticatorapp.User;
import com.firebase.ui.firestore.FirestoreRecyclerAdapter;
import com.firebase.ui.firestore.FirestoreRecyclerOptions;
public class UserAdapter extends FirestoreRecyclerAdapter<User, UserAdapter.ViewHolder> {
public UserAdapter(@NonNull FirestoreRecyclerOptions<User> options) {
super(options);
}
@Override
protected void onBindViewHolder(@NonNull ViewHolder holder, int position, @NonNull User model) {
holder.fName.setText(model.getfName());
holder.profile_image.setImageResource(R.mipmap.ic_launcher);
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.user_item, parent, false);
return new UserAdapter.ViewHolder(view);
}
class ViewHolder extends RecyclerView.ViewHolder{
public TextView fName;
public ImageView profile_image;
public ViewHolder(View itemView){
super(itemView);
fName = itemView.findViewById(R.id.username);
profile_image = itemView.findViewById(R.id.profile_image);
}
}
}
user_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp">
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/profile_image"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@mipmap/ic_launcher">
</de.hdodenhof.circleimageview.CircleImageView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="username"
android:id="@+id/username"
android:layout_toRightOf="@id/profile_image"
android:layout_toEndOf="@id/profile_image"
android:layout_marginLeft="10dp"
android:layout_centerVertical="true"
android:textSize="18sp"
android:textColor="#FFEB3B">
</TextView>
</RelativeLayout>
答案 0 :(得分:0)
在将readUsers()
设置为adapter
之前,请先呼叫recyclerView
,因为您需要先初始化适配器。
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_users, container, false);
RecyclerView recyclerView1 = view.findViewById(R.id.recyclerview);
recyclerView1.setHasFixedSize(true);
recyclerView1.setLayoutManager(new LinearLayoutManager(getContext()));
/******************/
readUsers();
recyclerView1.setAdapter(userAdapter);
/******************/
return view;
}
更新
您还需要像这样设置适配器的生命周期所有者
FirestoreRecyclerOptions<User> options = new FirestoreRecyclerOptions.Builder<User>()
.setQuery(userRef, User.class)
.setLifecycleOwner(this)
.build();