[英]Why my adapter inside the recyclerview did not display?
我想在parentHomeActivity中显示注册历史记录,所有这些数据都将在firebase数据库中获取。 我已经创建了适配器编码,还创建了central_registration_list_layout.xml以仅显示2个属性:
学生姓名(全名)
已注册的学费名称(tuitionName)。
没有红色错误,崩溃或任何日志记录。 我遇到的问题是,我的适配器未显示在我的应用程序中。 真希望有人可以教我,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);
}
}
}
parentHomeActivity编码如下所示:
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"/>
您应该这样存储数据
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());
}
使用以下方法检索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);
}
从
(0)mAdapters.notifyItemInserted;
至
mAdapters.notifyDataSetChanged();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.