如何使用Firebase数据填充Spinner?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何使用Firebase数据填充Spinner?相关的知识,希望对你有一定的参考价值。

我已经在SO上的线程上搜索了关于线程的文章,试图弄清楚我是如何做到这一点的,但是还没有更接近。

这个线程(How to get a String List from Firebase to fill a Spinner)似乎可能是有益的,但是我不确定如何为我自己的场景实现它。

我希望属性地址出现在微调器中。

如果问题看起来含糊不清,很难解释。

MaintenanceActivity

public class MaintenanceActivity extends AppCompatActivity {

EditText editTextTitle;
EditText editTextDesc;
Spinner spinnerPrimary;
Spinner spinnerSecondary;
Spinner spinnerProperty;
Button buttonSubmit;

DatabaseReference databaseMaintenance;

ListView listViewIssues;

List<Maintenance> maintenanceList;

fDatabaseRoot.child("propertyAddress").addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        // Is better to use a List, because you don't know the size 
        // of the iterator returned by dataSnapshot.getChildren() to
        // initialize the array
        final List<String> propertyAddress = new ArrayList<String>();

        for (DataSnapshot addressSnapshot: dataSnapshot.getChildren()) {
            String propertyAddress = addressSnapshot.child("propertyAddress").getValue(String.class);
            propertyAddress.add(propertyAddress);
        }

        Spinner spinnerProperty = (Spinner) findViewById(R.id.spinnerProperty);
        ArrayAdapter<String> addressAdapter = new ArrayAdapter<String>(MaintenanceActivity.this, android.R.layout.simple_spinner_item, areas);
        addressAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinnerProperty.setAdapter(addressAdapter);
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {

    }
});

activity_maintenance

<Spinner
    android:id="@+id/spinnerProperty"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:entries="@array/fuel" />

Firebase数据库

Firebase

错误

用这个答案得到几个错误。我以前遇到过这些错误,不知道如何解决它们。

Errors

LogCat错误

    03-01 17:19:53.402 28517-28517/com.example.***.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                       Process: com.example.***.myapplication, PID: 28517
                                                                                       java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
                                                                                           at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:445)
                                                                                           at android.widget.ArrayAdapter.getDropDownView(ArrayAdapter.java:493)
                                                                                           at android.widget.Spinner$DropDownAdapter.getDropDownView(Spinner.java:994)
                                                                                           at android.widget.Spinner$DropDownAdapter.getView(Spinner.java:990)
                                                                                           at android.widget.Spinner.measureContentWidth(Spinner.java:856)
                                                                                           at android.widget.Spinner$DropdownPopup.computeContentWidth(Spinner.java:1225)
                                                                                           at android.widget.Spinner$DropdownPopup.show(Spinner.java:1251)
                                                                                           at android.widget.Spinner.performClick(Spinner.java:780)
                                                                                           at android.support.v7.widget.AppCompatSpinner.performClick(AppCompatSpinner.java:442)
                                                                                           at android.view.View$PerformClick.run(View.java:24770)
                                                                                           at android.os.Handler.handleCallback(Handler.java:790)
                                                                                           at android.os.Handler.dispatchMessage(Handler.java:99)
                                                                                           at android.os.Looper.loop(Looper.java:164)
                                                                                           at android.app.ActivityThread.main(ActivityThread.java:6494)
                                                                                           at java.lang.reflect.Method.invoke(Native Method)
                                                                                           at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
                                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
03-01 17:19:53.403 28517-28517/com.example.***.myapplication E/UncaughtException: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
                                                                                              at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:445)
                                                                                              at android.widget.ArrayAdapter.getDropDownView(ArrayAdapter.java:493)
                                                                                              at android.widget.Spinner$DropDownAdapter.getDropDownView(Spinner.java:994)
                                                                                              at android.widget.Spinner$DropDownAdapter.getView(Spinner.java:990)
                                                                                              at android.widget.Spinner.measureContentWidth(Spinner.java:856)
                                                                                              at android.widget.Spinner$DropdownPopup.computeContentWidth(Spinner.java:1225)
                                                                                              at android.widget.Spinner$DropdownPopup.show(Spinner.java:1251)
                                                                                              at android.widget.Spinner.performClick(Spinner.java:780)
                                                                                              at android.support.v7.widget.AppCompatSpinner.performClick(AppCompatSpinner.java:442)
                                                                                              at android.view.View$PerformClick.run(View.java:24770)
                                                                                              at android.os.Handler.handleCallback(Handler.java:790)
                                                                                              at android.os.Handler.dispatchMessage(Handler.java:99)
                                                                                              at android.os.Looper.loop(Looper.java:164)
                                                                                              at android.app.ActivityThread.main(ActivityThread.java:6494)
                                                                                              at java.lang.reflect.Method.invoke(Native Method)
                                                                                              at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
                                                                                              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
答案

我可以帮助你,但你需要配置数据库和firebase,请参阅docs

在你的活动之后:

FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference fDatabaseRoot = database.getReference();

fDatabaseRoot.child("properties").addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            // Is better to use a List, because you don't know the size
            // of the iterator returned by dataSnapshot.getChildren() to
            // initialize the array
            final List<String> propertyAddressList = new ArrayList<String>();

            for (DataSnapshot addressSnapshot: dataSnapshot.getChildren()) {
                String propertyAddress = addressSnapshot.child("propertyAddress").getValue(String.class);
                if (propertyAddress!=null){
                    propertyAddressList.add(propertyAddress);
                }
            }

            Spinner spinnerProperty = (Spinner) findViewById(R.id.maintenanceProperty);
            ArrayAdapter<String> addressAdapter = new ArrayAdapter<String>(MaintenanceActivity.this, android.R.layout.simple_spinner_item, propertyAddressList);
            addressAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            spinnerProperty.setAdapter(addressAdapter);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

您已经为propertyAddress添加了一个侦听器,但是当数据快照返回时,您再次探测propertyAddress,这将无法工作,因为侦听器已经返回了属性地址。我上面写的方式将返回数据库中的所有键,在每个键中都有一个可以检索的propertyAddress。试试看。

以上是关于如何使用Firebase数据填充Spinner?的主要内容,如果未能解决你的问题,请参考以下文章

使用对象列表填充 Android Spinner

如何从firebase获取填充数据的表

Android/Spinner:删除箭头右侧的填充

我应该使用copyFromRealm()来填充Spinner吗?

如何从 tblContacts 中选择 android spinner

无法使用光标从数据库中成功检索Spinner选定的项目文本以填充我的应用程序中的联系人列表项目