【问题标题】:Android Google Firebase Database Querying on SpinnerSpinner 上的 Android Google Firebase 数据库查询
【发布时间】:2017-08-02 16:14:34
【问题描述】:

例如,我在数据库中有 4 个孩子:A、B、C、D。每个孩子都有一个名为“countryName”的属性。假设 A 有英国,B 有法国,C 有意大利,D 有意大利。这些子节点显示为列表,列表上方是带有 3 个选项的微调器 - 英国、法国、意大利。默认情况下,我在列表视图中看到所有 4 个孩子。假设我在 Spinner 上选择英国,然后我只看到对象 A。假设我在 Spinner 上选择意大利,然后我在列表视图中看到对象 C 和 D。我只是不知道如何使用各种参数调用对数据库的查询。这是我的微调器代码:

spinCountry = (Spinner) findViewById(R.id.spinCountry);

    ArrayAdapter<CharSequence> spinAdapter = ArrayAdapter.createFromResource(this,
            R.array.country_array, android.R.layout.simple_spinner_item);

    spinAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    spinCountry.setAdapter(spinAdapter);

    spinCountry.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

            //Get whatever is chosen on a spinner
            String spinnerCountry = parent.getItemAtPosition(position).toString();

            //Here is my query which on SQL would be 'SELECT * FROM THE DATABASE WHERE countryName EQUALS TO'country on a chosen on spinner'
            Query querySpinner = mLocationDatabaseReference.orderByChild("countryName").equalTo(spinnerCountry);

        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });

[修复]

因此,我按照 Frank van Puffelen 的建议添加了一个侦听器,设法解决了我的问题。

  mLocationAdapter.clear();

                querySpinner.addChildEventListener(new ChildEventListener() {
                    @Override
                    public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                        locationCurrent = dataSnapshot.getValue(LocationCurrent.class);
                        mLocationAdapter.add(locationCurrent);
                    }...
//Everything else is empty on addChildEventListener method so no point pasting it here

【问题讨论】:

  • 您的查询似乎没问题。你在问什么?如何实际从该查询中获取数据?
  • 您需要attach a listener 才能获得查询结果。有关查询的许多问题,请参阅stackoverflow.com/…。它们包含大量代码,您可以从中获得灵感。
  • 你能告诉我们你的 LocationCurrent 课程的内容吗?
  • 我的问题刚刚解决!谢谢你们。我以为我必须采取不同的方式,但有更简单的解决方案。请查看我更新的帖子,了解我是如何修复它的
  • 请将您的答案添加为下面的解决方案,而不是作为问题底部的更新,以便其他人更容易找到它,谢谢!

标签: android firebase firebase-realtime-database querying


【解决方案1】:

所以我设法通过按照 Frank van Puffelen 的建议添加一个监听器来解决我的问题。下面的代码在onItemSelectedmethod

mLocationAdapter.clear();

            querySpinner.addChildEventListener(new ChildEventListener() {
                @Override
                public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                    locationCurrent = dataSnapshot.getValue(LocationCurrent.class);
                    mLocationAdapter.add(locationCurrent);
                }...
//Everything else is empty on addChildEventListener method so no point pasting it here

【讨论】:

    猜你喜欢
    • 2017-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-08
    • 2017-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多