因此,我试图在片段中显示recyclerview及其内容。我的recyclerview显示正常,但我要在recyclerview内部显示的User对象却没有。
我从数据库中获得了用户名,并希望将其设置为要显示的文本视图。可以从Firebase完美检索该名称,因此它应该显示在recyclerview上。我已经将“ custom_lists_users.xml”(我的自定义视图显示给每个用户)膨胀到UserRecyclerAdap(我的适配器)类中。
但是,我假设它没有正确膨胀,因为如果膨胀了,那么我会在我膨胀的片段的recyclerview中看到我的用户名。
我附上了我认为问题所在的两节课,请告诉我是否遗漏了一些东西。谢谢!
UserRecyclerAdap类:
public class UserRecyclerAdap extends RecyclerView.Adapter<UserRecyclerAdap.ViewHolder> {
private Context context;
private List<User> users;
public UserRecyclerAdap(Context context, List<User> users) {
this.context = context;
this.users = users;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.custom_list_users, parent, false);
Log.d("layoutInflater", "inflated!");
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int i) {
String userName = users.get(i).getName();
holder.name.setText(userName);
Log.d("bindViewHolder", holder.name.toString());
}
@Override
public int getItemCount() {
return users.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView name;
ImageView profilePic;
public ViewHolder(@NonNull View itemView) {
super(itemView);
name = itemView.findViewById(R.id.name);
}
}
}
UserFragment类
public class UsersFragment extends Fragment {
private RecyclerView recyclerView;
private List<User> users;
private UserRecyclerAdap userRecyclerAdap;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_users, container, false);
recyclerView = view.findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(view.getContext()));
users = new ArrayList<>();
readUsers();
return view;
}
public void readUsers() {
final FirebaseUser firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("USERS");
//goes thru everythig stored in firebase database
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
users.clear();
for(DataSnapshot snapshot : dataSnapshot.getChildren()) {
//goes thru every object in db and makes a user obj out of data
User user = snapshot.getValue(User.class);
assert user != null;
assert firebaseUser != null;
if(user.getID().equals(firebaseUser.getUid())) {
users.add(user);
}
}
userRecyclerAdap = new UserRecyclerAdap(getContext(), users);
recyclerView.setAdapter(userRecyclerAdap);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
}
custom_list_users.xml文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="0dp">
<ImageView
android:id="@+id/profilePic"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:layout_marginStart="-1dp"
android:layout_marginLeft="-1dp"
android:layout_marginTop="3dp"
app:srcCompat="@drawable/ic_user2_foreground"
tools:srcCompat="@drawable/ic_user2_foreground" />
<TextView
android:id="@+id/name"
android:layout_width="192dp"
android:layout_height="wrap_content"
android:text="Name"
android:textSize="36sp" />
</LinearLayout>
</RelativeLayout>
请在下面将您的custom_list_users.xml文件替换为
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/profilePic"
android:layout_width="100dp"
android:layout_height="100dp"
app:srcCompat="@drawable/ic_user2_foreground"
tools:srcCompat="@drawable/ic_user2_foreground" />
<TextView
android:id="@+id/name"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="Name"
android:textSize="36sp" />
</LinearLayout>