【发布时间】:2021-07-21 02:57:22
【问题描述】:
所以我尝试使用 Firebase Ui-Database 通过分页适配器将数据检索到回收器视图。但是我一直给我错误,坚持了一个多星期。我认为查询中有错误。
所以 Model 类是 TeachersHelper 来检索数据。
Firebase 分页适配器:
public class teacher_adapter extends FirebaseRecyclerPagingAdapter<TeachersHelper, teacher_adapter.ExampleViewHolder> {
// variables to handle onclicks and dataloading
private ProgressBar progressBar;
private Context ctx;
// constructor
public teacher_adapter(@NonNull DatabasePagingOptions<TeachersHelper> options,
ProgressBar progressBar, Context ctx) {
super(options);
this.progressBar = progressBar;
this.ctx = ctx;
}
@NonNull
@Override
public ExampleViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_item, parent, false);
return new ExampleViewHolder(view, mListener);
}
@Override
protected void onBindViewHolder(@NonNull ExampleViewHolder holder, int position,
@NonNull TeachersHelper model) {
TeachersHelper currentItem = model;
// setting image
String imageLink = currentItem.getImage();
// Image assigning
if (currentItem.getImage().equals("No")) {
holder.iconName.setImageResource(R.drawable.profilepicture);
} else {
Picasso.get().load(imageLink).into(holder.iconName);
}
holder.individualName.setText(currentItem.gettName());
holder.departmentOrstudentstuff.setText(currentItem.gettDepartment());
}
@Override
protected void onLoadingStateChanged(@NonNull LoadingState state) {
switch (state) {
case LOADING_INITIAL:
// The initial load has begun
// ...
case LOADING_MORE:
progressBar.setVisibility(View.VISIBLE);
// The adapter has started to load an additional page
// ...
case LOADED:
progressBar.setVisibility(View.GONE);
// The previous load (either initial or additional) completed
// ...
case ERROR:
StyleableToast.makeText(ctx, "Error Try Again", R.style.exampleToast).show();
// The previous load (either initial or additional) failed. Call
// the retry() method in order to retry the load operation.
// ...
}
}
public static class ExampleViewHolder extends RecyclerView.ViewHolder {
public ImageView iconName;
public TextView individualName;
public TextView departmentOrstudentstuff;
public ExampleViewHolder(View itemView, final teacher_adapter.OnItemClickListener listener) {
super(itemView);
iconName = itemView.findViewById(R.id.profilePic_listView);
individualName = itemView.findViewById(R.id.studentsName_Listview);
departmentOrstudentstuff = itemView.findViewById(R.id.student_Registrattion_listview);
}
}
}
主要活动:
检索数据
// query
Query baseQuery = FirebaseDatabase.getInstance()
.getReference("Admin").child(mAuth.getUid()).child("INSTITUTE")
.child("TEACHERS").child("TEACHERINFORMATION");
//I think I have an error in defining the query
PagedList.Config config = new PagedList.Config.Builder()
.setEnablePlaceholders(false)
.setPrefetchDistance(10)
.setPageSize(20)
.build();
// The options for the adapter combine the paging configuration with query information
// and application-specific options for lifecycle, etc.
DatabasePagingOptions<TeachersHelper> options = new DatabasePagingOptions.Builder<TeachersHelper>()
.setLifecycleOwner(this)
.setQuery(baseQuery, config, TeachersHelper.class)
.build();
// assiging the adapter
teacher_adapter = new teacher_adapter(options, bottomProgressbar, teacher_admin.this);
teacher_adapter.notifyDataSetChanged();
ONSTART 和 ONSTOP:
@Override
protected void onStart() {
super.onStart();
teacher_adapter.startListening();
}
@Override
protected void onStop() {
super.onStop();
teacher_adapter.stopListening();
}
这是给定义适配器的错误加载数据TOAST。
【问题讨论】:
-
请注意,全大写的文本被大多数人认为是在大喊大叫。相反,您可以编辑您的问题以显示您在运行此代码时收到的实际错误消息吗?
-
对不起,我只是想强调一下,它显示了来自 onLoadingstate 的 toast 更改了从适配器加载数据的错误
-
不,它没有使用包装类进行锻炼。我所做的是我删除了复杂的节点结构,即我刚刚开始在 Teachers 节点下插入带有 push id 的数据。然后效果很好。谢谢你坦率????????。
标签: android firebase firebase-realtime-database android-recyclerview firebaseui