java - 回收者 View 不显示项目

标签 java android android-recyclerview

回收站 View 未显示项目。 datasnapshot2 正在接收正确的值。

child("Trips").child(tripID).child("Members") 正在获取 userID

enter image description here

        membersRecyclerView = findViewById(R.id.members);
        membersRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        membersRecyclerView.setItemAnimator(new DefaultItemAnimator());

        DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference().child("Trips").child(tripID).child("Members");

        databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                if (dataSnapshot.exists()) {
                    for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) {
                        DatabaseReference databaseReference1 = FirebaseDatabase.getInstance().getReference().child("Users")
                                .child(Objects.requireNonNull(dataSnapshot1.getKey()));
                        databaseReference1.addListenerForSingleValueEvent(new ValueEventListener() {
                            @Override
                            public void onDataChange(@NonNull DataSnapshot dataSnapshot2) {
                                UserDataModel userDataModel = dataSnapshot2.getValue(UserDataModel.class);
                                membersArrayList.add(userDataModel);
                            }

                            @Override
                            public void onCancelled(@NonNull DatabaseError databaseError) {

                            }
                        });

                    }
                    membersAdapter = new UserDataAdapter(membersArrayList);
                    membersRecyclerView.setAdapter(membersAdapter);
                }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
            }
        });

适配器


public class UserDataAdapter extends RecyclerView.Adapter<UserDataAdapter.ViewHolder> {

    private List<UserDataModel> MemberList;
    private TextView userName, userRating;

    UserDataAdapter(List<UserDataModel> MemberList) {
        this.MemberList = MemberList;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.members_list_item, parent, false);
        return new ViewHolder(view);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
        UserDataModel userDataModel = MemberList.get(position);
        userName.setText(userDataModel.getUserName());
        userRating.setText(userDataModel.getUserRating());
    }

    @Override
    public int getItemCount() {
        return MemberList.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        ViewHolder(View itemView) {
            super(itemView);
            userImage = itemView.findViewById(R.id.m_user_image);
            userName = itemView.findViewById(R.id.m_user_name);
            userRating = itemView.findViewById(R.id.m_user_rating);
        }
    }
}

用户数据模型

public class UserDataModel {
    public String userName, userEmail, contactNumber, gender, dateOfBirth, location, userID, createdDate, createdTime;
    int userRating;

    public UserDataModel() {

    }

    UserDataModel(String userName, String userEmail, String contactNumber, String gender, String dateOfBirth, String location, String userID, String createdDate, String createdTime) {
        this.userName = userName;
        this.userEmail = userEmail;
        this.contactNumber = contactNumber;
        this.gender = gender;
        this.dateOfBirth = dateOfBirth;
        this.location = location;
        this.userID = userID;
        this.createdDate = createdDate;
        this.createdTime = createdTime;
    }

    String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    String getUserEmail() {
        return userEmail;
    }

    public void setUserEmail(String userEmail) {
        this.userEmail = userEmail;
    }

    String getContactNumber() {
        return contactNumber;
    }

    public void setContactNumber(String contactNumber) {
        this.contactNumber = contactNumber;
    }

    String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    String getDateOfBirth() {
        return dateOfBirth;
    }

    public void setDateOfBirth(String dateOfBirth) {
        this.dateOfBirth = dateOfBirth;
    }

    String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }

    String getUserID() {
        return userID;
    }

    public void setUserID(String userID) {
        this.userID = userID;
    }

    public String getCreatedDate() {
        return createdDate;
    }

    public void setCreatedDate(String createdDate) {
        this.createdDate = createdDate;
    }

    public String getCreatedTime() {
        return createdTime;
    }

    public void setCreatedTime(String createdTime) {
        this.createdTime = createdTime;
    }

    public int getUserRating() {
        return userRating;
    }

    public void setUserRating(int userRating) {
        this.userRating = userRating;
    }
}

xml(删除了一些内容)

<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:orientation="vertical"
    tools:context=".TripActivity">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

<LinearLayout
                    android:id="@+id/createdBy"
                    android:layout_width="match_parent"
                    android:layout_height="110dp">

                    <de.hdodenhof.circleimageview.CircleImageView
                        android:id="@+id/createdBy_user_image"
                        android:layout_width="95dp"
                        android:layout_height="95dp"
                        android:layout_marginHorizontal="10dp"
                        android:layout_marginTop="10dp"
                        android:scaleType="centerCrop"
                        android:src="@color/black" />

                <View
                    android:layout_width="match_parent"
                    android:layout_height="1dp"
                    android:layout_marginTop="10dp"
                    android:background="@color/darkgrey" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="20dp"
                    android:fontFamily="@font/heebo_medium"
                    android:text="Trip Description"
                    android:textColor="@color/black"
                    android:textSize="16sp" />

                <TextView
                    android:id="@+id/trip_description"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:fontFamily="@font/heebo_light"
                    android:text="This is the description of trip." />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="20dp"
                    android:fontFamily="@font/heebo_medium"
                    android:text="Members"
                    android:textColor="@color/black"
                    android:textSize="16sp" />

                <androidx.recyclerview.widget.RecyclerView
                    android:id="@+id/members"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:scrollbars="none" />
            </LinearLayout>
        </LinearLayout>
    </ScrollView>
</RelativeLayout>

最佳答案

我看到您在类中添加了许多未使用的字段UserDataModel:

尝试更改您的UserDataModel:

public class UserDataModel {

//these must be named exactly like your database, and the exact same number
private String userName;
private String userEmail;
private String userID;
private String contactNumber;
private String gender;
private String dateOfBirth;
private String location;


//constructor

public UserDataModel(String userName,String userEmail,String userID,String contactNumber,String gender,String dateOfBirth,String location){

this.userName = userName;
this.userEmail = userEmail;
this.userID = userID;
this.contactNumber=contactNumber;
this.gender = gender;
this.dateOfBirth = dateOfBirth;
this.location = location;

}

//generate also getters and setters........

}

您还必须做的一件事:

你看到这些代码行了吗:

membersAdapter = new UserDataAdapter(membersArrayList);
membersRecyclerView.setAdapter(membersAdapter);

将它们直接添加到这些行下:

@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot2) {
UserDataModel userDataModel = dataSnapshot2.getValue(UserDataModel.class);
membersArrayList.add(userDataModel);

//here......

membersAdapter = new UserDataAdapter(membersArrayList);
membersRecyclerView.setAdapter(membersAdapter);
.....
.....
.....

更新2:

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/members"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                ...............

关于java - 回收者 View 不显示项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61549323/

相关文章:

java - 如何使 JPopupMenu 始终如一地变为 "shadow"组件

java - 尝试注册 XMPP

Android AppCompat 模块 Activity 不使用 list 上定义的样式

android - AGPBI : Error retrieving parent for item: No resource found

android - RecyclerView onBindViewHolder 导致严重的性能问题

android - Recycler View 如何绘制单独的行

java - 你能破坏 Scala 单例(对象)吗?

java - Hibernate,JPA无法删除一对多关系

android - 如何为 ExpandableListView 设置组分隔线颜色

java - 将 RecyclerView 中的项目更改为 onBindViewHolder