【问题标题】:Check if no documents are returned against a Firebase ui query class检查是否没有针对 Firebase ui 查询类返回任何文档
【发布时间】:2019-05-18 06:10:09
【问题描述】:

我正在使用 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);
            }
        });

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

【问题讨论】:

  • 问题解决了吗?
  • 没有,没有人回复兄弟
  • 我猜你有我的问题。
  • 您检查过 this 了吗?如果不起作用,请同时添加您的数据库结构和ExploreModel 类的内容。

标签: java android firebase google-cloud-firestore firebaseui


【解决方案1】:

您什么也得不到,因为模型类中的字段名称与数据库中字段的名称匹配。见mDescriptiondescription?两者必须相同。

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

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

【讨论】:

  • 我的问题是在特定集合中可能没有可用的文档imgur.com/a/Cy6LA6V 只需看这张图片,杂货店集合中有三个文档。
  • 查询查询 = db.collection("cities/"+city+"/"+category);我想知道我的查询对象是否为空
  • 您的query 对象永远不会是null,因为您已经创建了它的新对象。如果您想知道它是否返回一些结果,您需要使用侦听器或验证您的DocumentSnapshot 是否存在。如果您对字段使用实际命名,您将永远不会得到任何结果。所以为了解决这个问题,你首先需要改变你的领域,对吧?
猜你喜欢
  • 2021-09-12
  • 2020-10-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-23
  • 2020-03-09
  • 2014-02-15
相关资源
最近更新 更多