检查是否没有针对 Firebase ui 查询 class 返回任何文档

Check if no documents are returned against a Firebase ui query class

我正在将 Firebase-UI 与 firestore 一起使用,现在有可能没有针对特定查询返回任何文档,那么我怎么知道呢?

  progressBar.setVisibility(View.VISIBLE);
        SharedPreferences prefs = getActivity().getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE);
        String city = prefs.getString("selectedCity", "pakistan");
        Toast.makeText(getActivity(), city, Toast.LENGTH_SHORT).show();
        Query query = db.collection("cities/"+city+"/"+category);

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



        recyclerView.setAdapter(adapter);
        progressBar.setVisibility(View.GONE);
        adapter.setOnItemClickListener(new ExploreAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(DocumentSnapshot documentSnapshot, int position) {
                // ExploreModel exploreModel = documentSnapshot.toObject(ExploreModel.class);
                // String id = documentSnapshot.getId();

                String path = documentSnapshot.getReference().getPath();
                Toast.makeText(getActivity(), "Position" + position + " path :" + path, Toast.LENGTH_SHORT).show();
                Intent detailActivityIntent = new Intent(getActivity(), SingleItemDetailsActivity.class);
                detailActivityIntent.putExtra("document_path", path);
                detailActivityIntent.putExtra("category",category);
                startActivity(detailActivityIntent);
            }
        });

如果没有返回结果/文档,就会出现空白屏幕,所以我想显示一个适当的空白屏幕等

您没有得到任何东西,因为模型中的字段名称 class 与数据库中的字段名称匹配。看到 mDescriptiondescription?两者必须相同。

要解决此问题,您可以将模型 class 更改为如下所示:

public class ExploreModel {
    private String title, description, imageUrl, location;

    public ExploreModel() {}

    public ExploreModel(String title, String description, String imageUrl, String location) {
        this.title = title;
        this.description = description;
        this.imageUrl = imageUrl;
        this.location = location;
    }

    public String getTitle() { return title; }
    public String getDescription() { return description; }
    public String getImageUrl() { return imageUrl; }
    public String getLocation() { return location; }
}

或者您可以使用注释:

public class ExploreModel {
    private String title, description, imageUrl, location;

    public ExploreModel() {}

    public ExploreModel(String title, String description, String imageUrl, String location) {
        this.title = title;
        this.description = description;
        this.imageUrl = imageUrl;
        this.location = location;
    }

    @PropertyName("mTitle")
    public String getTitle() { return title; }
    @PropertyName("mDescription")
    public String getDescription() { return description; }
    @PropertyName("mImageUrl")
    public String getImageUrl() { return imageUrl; }
    @PropertyName("mLocation")
    public String getLocation() { return location; }
}