Firebase 分页回收器适配器给出错误?

Firebase Paging Recycler Adapter Giving Error?

所以我尝试使用 Firebase Ui-Database 通过分页适配器将数据检索到回收站视图。但我一直给我错误,坚持了一个多星期。我认为查询有误。

所以模型 class 是检索数据的 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 时出现错误。

您将引用创建为:

FirebaseDatabase.getInstance()
            .getReference("Admin").child(mAuth.getUid()).child("INSTITUTE")
            .child("TEACHERS").child("TEACHERINFORMATION");

但是您的数据库中 TEACHERS 下没有子 TEACHERINFORMATION,因此引用指向您的数据库中没有数据。

我猜您希望适配器显示 TEACHERS:

下的数据
FirebaseDatabase.getInstance()
            .getReference("Admin").child(mAuth.getUid()).child("INSTITUTE")
            .child("TEACHERS");

然后对于每个子节点,它应该显示 TEACHERINFORMATION 个子节点。这意味着 TEACHERINFORMATION 需要成为您用来将 DataSnapshot 映射到您的应用程序代码的 Java class 的一部分。

因此,如果您当前的 TeachersHelper 包含 imagetDepartmentTEACHERINFORMATION 的其他属性,您可以为此创建一个包装器 class:

public class TeacherWrapper {
   @PropertyName("TEACHERINFORMATION")
   public TeachersHelper TEACHERINFORMATION;
}

将此 class 传递到您的适配器中:

.setQuery(baseQuery, config, TeacherWrapper.class)

然后修改您的其他代码以从包装器中获取嵌套数据。

尝试使用简单的数据库架构。所做的是删除 Teacherinformation 节点并开始在 pushid 下面插入数据,结果很好。