java - 无法在 UserRecyclerAdapter 类中使用 "custom_list_users.xml"来膨胀 fragment

标签 java android android-fragments android-recyclerview

所以我试图在 fragment 中显示 recyclerview 及其内容。我的 recyclerview 显示正常,但我想在 recyclerview 内显示的 User 对象却显示不正常。

我从数据库中获取了用户名,并希望将其设置为 TextView 来显示。该名称正在从 Firebase 中完美检索,因此它应该显示在回收器 View 上。我已将“custom_lists_users.xml”(我的自定义 View 来显示每个用户)膨胀到 UserRecyclerAdap (我的适配器)类中。

但是,我假设它没有正确膨胀,因为如果它正确膨胀,那么我会在我膨胀的 fragment 的回收器 View 中看到我的用户名。

我附加了两个我认为有问题的类(class),如果我遗漏了什么,请告诉我。谢谢!

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>

关于java - 无法在 UserRecyclerAdapter 类中使用 "custom_list_users.xml"来膨胀 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61814588/

相关文章:

android - Python - Kivy 是否在 Android 应用程序中实现 Activity ?

android - 查看分页器仅显示 1 个 ListView

安卓重启日历

android - 在 ActionBar 选项卡下的 fragment 中添加/替换 fragment

java - 公共(public)学生retrieveById(int id)

android - 连接在请求完成之前关闭 - Android ION

java - 如何让 java 在将输入分配给变量之前等待输入以避免 java.util.NoSuchElementException

Android 检测括号和特殊字符输入,在 api 11 之前

java - H2OServerError : Server process terminated with error code 1 in Python on Mac while running h2o. 初始化()

Java - 将用户输入放入数组中