【问题标题】:Firebase Paging Recycler Adapter Giving Error?Firebase 分页回收器适配器出现错误?
【发布时间】: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


【解决方案1】:

您将参考创建为:

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 类的一部分。

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

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

将此类传递给您的适配器:

.setQuery(baseQuery, config, TeacherWrapper.class)

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多