gpt4 book ai didi

java - 如何查看 FirestoreRecyclerAdapter 是否为空?

转载 作者:行者123 更新时间:2023-12-02 09:34:41 26 4
gpt4 key购买 nike

我想知道如何才能发现 FirestoreRecyclerAdapter 仅是空的。通过下面的代码,我可以发现它是否有一个或多个实例,但它不会准确地告诉我它什么时候里面没有对象。验证 if(adapter.getItemCount == 0) 每次都会显示,因为适配器每次都会通过 0 计数,所以显然这个函数不起作用。

那么如何才能发现它是否完全为空呢?

这是我的代码:

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() 并 checkin

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();
}
};

关于java - 如何查看 FirestoreRecyclerAdapter 是否为空?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57630866/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com