带有数据库数组的 NumberPicker

NumberPicker with array from database

我想帮助我开发一个带有从 firebase 获得的列表的数字选择器,这段代码现在给我一个错误,如果我删除 "setMaxValue and MinValue" 它只会给我带来一个值。

    CBSList = new ArrayList<>();
    DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Posts").child(post.getIDPost()).child("CBS");

    reference.addValueEventListener( new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            CBSList.clear();
            for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                CBSList.add(snapshot.getKey());

                String array[] = new String[CBSList.size()];
                for(int j =0;j<CBSList.size();j++){
                    array[j] = CBSList.get(j);
                }

                numberPicker.setMaxValue(CBSList.size() - 1);
                numberPicker.setMinValue(0);
                numberPicker.setDisplayedValues(array);
                numberPicker.setWrapSelectorWheel(true);
            }
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    } );

如果我离开 min 和 max 两行会出错

E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.ibra.kl, PID: 13463 java.lang.ArrayIndexOutOfBoundsException: length=1; index=1 at android.widget.NumberPicker.ensureCachedScrollSelectorValue(NumberPicker.java:1900) at android.widget.NumberPicker.initializeSelectorWheelIndices(NumberPicker.java:1715) at android.widget.NumberPicker.setMaxValue(NumberPicker.java:1471) at com.example.ibra.kl.ProductoPostActivity.onDataChange(ProdActivity.java:143) at com.google.firebase.database.core.ValueEventRegistration.fireEvent(com.google.firebase:firebase-database@@16.1.0:75) at com.google.firebase.database.core.view.DataEvent.fire(com.google.firebase:firebase-database@@16.1.0:63) at com.google.firebase.database.core.view.EventRaiser.run(com.google.firebase:firebase-database@@16.1.0:55) at android.os.Handler.handleCallback(Handler.java:789) at android.os.Handler.dispatchMessage(Handler.java:98) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6944) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)

您正在设置 setMaxValuesetMinValue,然后 CBSList 完全填满。

像这样更改您的代码

reference.addValueEventListener( new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        CBSList.clear();
        for (DataSnapshot snapshot : dataSnapshot.getChildren()) {

            CBSList.add(snapshot.getKey());
        }

        String array[] = new String[CBSList.size()];

        for(int j =0;j<CBSList.size();j++){
            array[j] = CBSList.get(j);
        }

        if (CBSList.size() > 1) {
            numberPicker.setMaxValue(CBSList.size() - 1);
            numberPicker.setMinValue(0);
            numberPicker.setDisplayedValues(array);
            numberPicker.setWrapSelectorWheel(true);
        }
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {

    }
} );