如何查看FirestoreRecyclerAdapter是否为空? [英] How to see if FirestoreRecyclerAdapter is empty?

查看:53
本文介绍了如何查看FirestoreRecyclerAdapter是否为空?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何找到 FirestoreRecyclerAdapter 仅为空的情况.通过下面的代码,我可以发现它是否包含一个或多个实例,但是当它里面没有对象时,它不会告诉我确切的信息.验证 if(adapter.getItemCount == 0)每次都会显示,因为适配器每次都会通过0计数,因此显然此功能不起作用.

I would like to know how is it possible to discover when FirestoreRecyclerAdapter is ONLY empty. With the following code, I can discover if it has one or more instances into it, but it won't tell me exactly when it has no objects inside. The verification if(adapter.getItemCount == 0) displays everytime because adapter passes through the 0 count everytime, so apparently this function won't work.

那么如何发现它是否完全空了?

So how is it possible to discover if it is totally empty?

这是我的代码:

Query query = spotsCollection.whereEqualTo("cityId", cityId).limit(5);

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

FirestoreRecyclerAdapter<Spots, SpotViewHolder> adapter = new FirestoreRecyclerAdapter<Spots, SpotViewHolder>(options) {
    @Override
    protected void onBindViewHolder(@NonNull SpotViewHolder spotViewHolder, int i, @NonNull Spots spots) {
        if(i > 3)
            // Do something with i > 3
    }

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

if(adapter.getItemCount() == 0)
    Toast.makeText(getContext(), "Empty", Toast.LENGTH_SHORT).show();

推荐答案

覆盖onDataChanged()并检入

Override onDataChanged() and check in it

FirestoreRecyclerAdapter<Spots, SpotViewHolder> adapter = new FirestoreRecyclerAdapter<Spots, SpotViewHolder>(options) {
    @Override
    protected void onBindViewHolder(@NonNull SpotViewHolder spotViewHolder, int i, @NonNull Spots spots) {
    //...
    }

    @NonNull
    @Override
    public SpotViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    //...
    }


    // Add this
    @Override
    public void onDataChanged() {
    // do your thing
    if(getItemCount() == 0)
    Toast.makeText(getContext(), "Empty", Toast.LENGTH_SHORT).show();
    }
};

这篇关于如何查看FirestoreRecyclerAdapter是否为空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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