如何使用Firebase数据填充Spinner?

时间:2018-03-01 15:32:33

标签: java android firebase firebase-realtime-database android-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)

1 个答案:

答案 0 :(得分:0)

我可以帮助您,但您需要配置数据库和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。试一试。