为什么我的适配器在 recyclerview 里面没有显示?

Why my adapter inside the recyclerview did not display?

我想在 parentsHomeActivity 中显示注册历史记录,所有这些数据都将在 firebase 数据库中获取。我创建了适配器编码,还创建了 recent_registration_list_layout.xml 以仅显示 2 个属性:-

  1. 学生姓名(全名)

  2. 已注册的学费名称(tuitionName)。

没有红色错误、崩溃或任何 logcat。我遇到的问题是,我的适配器没有显示在我的应用程序中。 Android Studio 的新手,真心希望有人能教教我。提前致谢。

数据库结构如下,我只想显示红圈属性:-

适配器编码如下:-

public class RegistrationHistoryAdapter extends RecyclerView.Adapter<RegistrationHistoryAdapter.ChildrenViewHolder>
{
    private Context mCtx;
    private List<StudentRegistration> childrenList;

public RegistrationHistoryAdapter(Context mCtx, List<StudentRegistration> childrenList)
{
    this.mCtx = mCtx;
    this.childrenList = childrenList;
}

@NonNull
@Override
public ChildrenViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
{
    LayoutInflater inflater = LayoutInflater.from(mCtx);
    View view = inflater.inflate(R.layout.recent_registration_list_layout, parent, false);
    return new ChildrenViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull final ChildrenViewHolder holder, final int position)
{
    final StudentRegistration studentRegistration = childrenList.get(holder.getAdapterPosition());
    holder.tuitioNameView.setText(studentRegistration.getTuitioname());
    holder.studNameView.setText(studentRegistration.getFullname());
}

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

class ChildrenViewHolder extends RecyclerView.ViewHolder
{
    TextView tuitioNameView, studNameView;
    CardView childrenLayout;

    public ChildrenViewHolder(View itemView)
    {
        super(itemView);
        tuitioNameView = itemView.findViewById(R.id.tvTuitionName);
        studNameView = itemView.findViewById(R.id.tvFullName);
        childrenLayout = itemView.findViewById(R.id.cvChildren);
    }
}
}

parentsHomeActivity编码如下:-

databaseReference.child("Student Registration").child(mAuth.getUid()).addValueEventListener(new ValueEventListener()
    {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot)
        {
            //Iterating through all the values in database
            mChildrenList = new ArrayList<>();
            for (DataSnapshot postSnapshot : dataSnapshot.getChildren())
            {
                StudentRegistration studentRegistration = postSnapshot.getValue(StudentRegistration.class);
                mChildrenList.add(0, studentRegistration);
            }

            //Creating adapter
            mAdapters = new RegistrationHistoryAdapter(getApplicationContext(), mChildrenList);

            //Adding adapter to recyclerview
            mRecyclerView.setAdapter(mAdapters);
            mAdapters.notifyItemInserted(0);
        }

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

        }
    });

    //other method
    mRecyclerView.setHasFixedSize(true); //set fixed size for element in recycler view
    mRecyclerView.setLayoutManager(new LinearLayoutManager(this));

xml 中的 Recycleview 布局如下所示:-

<android.support.v7.widget.RecyclerView
        android:id="@+id/rvChildren"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:background="@drawable/roundedbutton8"/>

使用此方法检索 firebase 数据:

ref.child("Student Registeration").addListenerForSingleValueEvent(new 
  ValueEventListener() {
 @Override
  public void onDataChange(DataSnapshot dataSnapshot) {
     if(dataSnapshot.getValue != null) {
       for(DataSnapshot ds : dataSnapshot.getChildren()) {
            //get your object data from ds and add it to array list
        }
  //set adapter now
}
}

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

改变 来自

for (DataSnapshot postSnapshot : dataSnapshot.getChildren())
            {
                StudentRegistration studentRegistration = postSnapshot.getValue(StudentRegistration.class);
                mChildrenList.add(0, studentRegistration);
            }

for (DataSnapshot postSnapshot : dataSnapshot.getChildren())
            {
                StudentRegistration studentRegistration = postSnapshot.getValue(StudentRegistration.class);
                mChildrenList.add(studentRegistration);
            }

mAdapters.notifyItemInserted(0);

mAdapters.notifyDataSetChanged();

你应该这样存储数据

    String current_user = FirebaseAuth.getInstance().getCurrentUser().getUid();
    mDatabase.child("Student Registration").child(current_user).child(uid).setValue(studentRegistration).addOnCompleteListener(new OnCompleteListener<Void>()
    {
        @Override
        public void onComplete(@NonNull Task<Void> task)
        {
            if(task.isSuccessful())
            {
                Toast.makeText(RegisterActivity.this, "Successfully registered!", Toast.LENGTH_SHORT).show();
                startActivity(new Intent(RegisterActivity.this, FinishActivity.class));
                finish();
            }

            else
            {
                Toast.makeText(RegisterActivity.this, task.getException().getMessage(), Toast.LENGTH_SHORT).show();
            }
            loadingBar.dismiss();
        }
    });

然后是获取数据。使用此代码

    String current_user = FirebaseAuth.getInstance().getCurrentUser().getUid();


    databaseReference.child("Student Registration").child(current_user).addValueEventListener(new ValueEventListener()
    {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot)
        {
            //Iterating through all the values in database
            mChildrenList = new ArrayList<>();
            for (DataSnapshot postSnapshot : dataSnapshot.getChildren())
            {
                StudentRegistration studentRegistration = postSnapshot.getValue(StudentRegistration.class);
                mChildrenList.add(studentRegistration);
            }

            //Creating adapter
            mAdapters = new RegistrationHistoryAdapter(getApplicationContext(), mChildrenList);

            //Adding adapter to recyclerview
            mRecyclerView.setAdapter(mAdapters);
        }

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

        }
    });

在您的适配器中将 onBindViewHolder 更改为

@Override
public void onBindViewHolder(@NonNull final ChildrenViewHolder holder, final int position)
{
    holder.tuitioNameView.setText(childrenList.get(holder.getAdapterPosition()).getTuitioname());
    holder.studNameView.setText(childrenList.get(holder.getAdapterPosition()).getFullname());
}