微博上的Android Google Firebase数据库查询

时间:2017-03-12 16:51:01

标签: android firebase firebase-realtime-database querying

例如我在数据库中有4个孩子:A,B,C,D。每个孩子都有一个名为'countryName'的属性。假设A有英国,B有法国,C有意大利,D有意大利。这些childer显示为列表,列表上方是旋转器,有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) {

        }
    });

[FIX]

所以我设法通过添加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

1 个答案:

答案 0 :(得分:1)

所以我设法通过添加Frank van Puffelen建议的监听器来解决我的问题。以下代码位于onItemSelected方法

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