Firestore RecyclerView itemCount返回0 [英] Firestore RecyclerView itemCount returns 0

查看:313
本文介绍了Firestore RecyclerView itemCount返回0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,在我的应用中,我正在使用 Firestore Recyclerview ,并且我希望计算其中显示的项目.我尝试使用 registerAdapterDataObserver onItemRangeInserted ,并且它们似乎仅在其中至少有一项有效 RecyclerView .如果没有,它甚至不会运行.

So in my app, I'm using Firestore Recyclerview and I want to count the items showed in it. I've tried using registerAdapterDataObserver and onItemRangeInserted and they seem to work ONLY if there is at least one item in the RecyclerView. If there is none it doesn't even run.

然后,我尝试实现 getItemCount() 方法,但它始终返回0.

Then, I've tried to implement the getItemCount() method but it always returns 0.

我有一个类,它是FirestoreRecyclerView的适配器.

I have a class which is the adapter for the FirestoreRecyclerView.

private OnItemClickListener listener;

public JobsAdapter(@NonNull FirestoreRecyclerOptions<Job> options) {
    super(options);
}

@Override
protected void onBindViewHolder(@NonNull jobHolder holder, int position, @NonNull Job model) {
    holder.textViewTitle.setText(model.getJobName());
    holder.textViewDescription.setText(model.getJobDescription());
    holder.textViewPostDate.setText(model.getJobDate());
}

@Override
public int getItemCount() {
    Log.d("adaptercount", "getItemCount: " + super.getItemCount());
    return super.getItemCount();
}


@NonNull
@Override
public jobHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.job_item, parent, false);
    return new jobHolder(v);
}

class jobHolder extends RecyclerView.ViewHolder {
    TextView textViewTitle;
    TextView textViewDescription;
    TextView textViewPostDate;

    public jobHolder(View itemView) {
        super(itemView);
        textViewTitle = itemView.findViewById(R.id.text_view_title);
        textViewDescription = itemView.findViewById(R.id.text_view_desc);
        textViewPostDate = itemView.findViewById(R.id.text_view_post_date);

        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = getAdapterPosition();
                if (position != RecyclerView.NO_POSITION && listener != null) {
                    listener.onItemClick(getSnapshots().getSnapshot(position), position);
                }
            }
        });
    }
}

getItemCount() 方法在这里非常有效,但是当我尝试在另一个片段中调用此方法时,它将返回0.

Here the getItemCount() method works perfectly, but when I try to call this method in another fragment it returns 0.

我的 RecyclerViewInit() 方法:

My RecyclerViewInit() method in the fragment:

Query query = prevJobRef.whereEqualTo("jobIsClosed", false).whereEqualTo("jobSenderID", userID);

    FirestoreRecyclerOptions<Job> options = new FirestoreRecyclerOptions.Builder<Job>().
            setQuery(query, Job.class)
            .build();

    adapter = new JobsAdapter(options);
    RecyclerView recyclerView = view.findViewById(R.id.recycler_view_activeJobs);
    recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    recyclerView.setAdapter(adapter);

    adapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
        public void onItemRangeInserted(int positionStart, int itemCount) {
            Log.d("count", String.valueOf(adapter.getItemCount()));
       
        }
    });

    Log.d("count2", String.valueOf(adapter.getItemCount()));

  
}

我感觉这是一个业余错误,希望你们能帮助我!

I have a feeling that it's an amateur mistake and I hope you guys can help me out!

推荐答案

我尝试使用 registerAdapterDataObserver onItemRangeInserted ,并且它们似乎仅在至少存在 RecyclerView 中的一项.如果没有,甚至无法运行.

I've tried using registerAdapterDataObserver and onItemRangeInserted and they seem to work ONLY if there is at least one item in the RecyclerView. If there is none it doesn't even run.

这是预期的行为,因为当在适配器中插入至少一项时, onItemRangeInserted()方法仅触发 .如果没有要插入的项目,则将永远不会触发 onItemRangeInserted()方法.但是,有两种方法可以解决此问题.第一个方法是使用与 onItemRangeInserted()相同的方法来覆盖 onChanged()方法,因为即使没有项目,它也会被触发.第二个选择是直接在适配器类中覆盖 onDataChanged()方法,如以下帖子中我的回答所述:

That's the expected behavior since onItemRangeInserted() methods fires only when at least one item is inserted in the adapter. If there is no item to be inserted, the onItemRangeInserted() method will never fire. However, there are two ways in which you can solve this problem. The first one would be to override the onChanged() method in the same way you did onItemRangeInserted(), as it will be triggered even if there no items. And the second option that you have is to override the onDataChanged() method directly in your adapter class, as explained in my answer from the following post:

Android,从Firebase加载数据完成后

仅当您使用Firebase-UI库时,第二个选项才可能.

The second option is possible only because you are using the Firebase-UI library.

这篇关于Firestore RecyclerView itemCount返回0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆