【问题标题】:Why my adapter inside the recyclerview did not display?为什么我的recyclerview里面的适配器没有显示?
【发布时间】:2019-05-04 13:48:48
【问题描述】:

我想在 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));

Recycleview xml 布局如下所示:-

<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"/>

【问题讨论】:

  • 您能否使用 RecyclerView 发布您尝试将适配器设置为的 XML 代码?
  • 检查你是否从firebase接收
  • @KacperKogut 已经编辑问题..
  • 您确定DataSnapshot 包含任何数据吗?
  • @Adda 使用这样的日志来检查 Log.e("data", String.valueOf(mChildrenList));

标签: android firebase firebase-realtime-database android-recyclerview android-adapter


【解决方案1】:

你应该这样存储数据

    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());
}

【讨论】:

  • 但是先生.. 这部分? StudentRegistration studentRegistration = new StudentRegistration(uid, tutorialame, fullname, icnum, homeaddress, parentname, parentcontactnum, userUid, selectedTuitionPackage);
  • 其他的都和你的一样。我刚刚拿起你应该改变的部分
  • 我的应用程序崩溃先生...进程:com.addaj.mobilerecommendationsystem,PID:32093 java.lang.NullPointerException:尝试调用虚拟方法'void android.widget.TextView.setText(java.lang .CharSequence)' 在 com.addaj.mobilerecommendationsystem.adapters.RegistrationHistoryAdapter.onBindViewHolder(RegistrationHistoryAdapter.java:17) 的 com.addaj.mobilerecommendationsystem.adapters.RegistrationHistoryAdapter.onBindViewHolder(RegistrationHistoryAdapter.java:41) 的空对象引用上>
  • 更新代码。你的onBindViewHolder 不正确
  • 先生......我的应用程序仍然崩溃,我的编码混乱已经尝试了很多方法嗯。先生,您现在有空吗?我们可以连接一会儿吗?这样你就可以看到我最新的编码hmm:(
【解决方案2】:

改变 来自

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();

【讨论】:

  • 从最终的 StudentRegistration 更改 studentRegistration = childrenList.get(holder.getAdapterPosition()); TO final StudentRegistration studentRegistration = childrenList.get(position);在适配器 onbindView
【解决方案3】:

使用此方法检索 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) {
  // ...
}
});

【讨论】:

  • BTW android studio 警告使用holder.getAdapterPosition() 而不是position
  • @Adda 所以我认为数据没有收到
  • 学生注册的孩子在您使用 mAuth.getUid 时使用推送 ID
  • 这就是问题所在。您使用了错误的 mAuth.getUid。
  • @ZarSaeed 好的先生。先生,我可以要您的电子邮件,以便我们可以使用 chrome 桌面连接一段时间...
猜你喜欢
  • 1970-01-01
  • 2023-03-17
  • 2016-10-30
  • 2021-10-10
  • 1970-01-01
  • 2018-01-26
  • 1970-01-01
  • 2023-01-15
  • 1970-01-01
相关资源
最近更新 更多