NumberPicker与数据库中的数组

时间:2019-04-12 17:05:40

标签: java android firebase numberpicker

我想帮助我开发一个从firebase获得的列表的数字选择器,此代码现在给我一个错误,如果删除“ setMaxValue和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) {

        }
    } );

如果我留下最小和最大两行,则会出错

  

E / Android运行时:致命异常:主要       流程:com.example.ibra.kl,PID:13463       java.lang.ArrayIndexOutOfBoundsException:length = 1;索引= 1           在android.widget.NumberPicker.ensureCachedScrollSelectorValue(NumberPicker.java:1900)           在android.widget.NumberPicker.initializeSelectorWheelIndices(NumberPicker.java:1715)           在android.widget.NumberPicker.setMaxValue(NumberPicker.java:1471)           在com.example.ibra.kl.ProductoPostActivity $ 2.onDataChange(ProdActivity.java:143)           com.google.firebase.database.core.ValueEventRegistration.fireEvent(com.google.firebase:firebase-database @@ 16.1.0:75)           com.google.firebase.database.core.view.DataEvent.fire(com.google.firebase:firebase-database @@ 16.1.0:63)           com.google.firebase.database.core.view.EventRaiser $ 1.run(com.google.firebase:firebase-database @@ 16.1.0:55)           在android.os.Handler.handleCallback(Handler.java:789)           在android.os.Handler.dispatchMessage(Handler.java:98)           在android.os.Looper.loop(Looper.java:164)           在android.app.ActivityThread.main(ActivityThread.java:6944)           在java.lang.reflect.Method.invoke(本机方法)           在com.android.internal.os.Zygote $ MethodAndArgsCaller.run(Zygote.java:327)           在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)

1 个答案:

答案 0 :(得分:0)

您要在setMaxValue完全填满之前设置setMinValueCBSList

像这样更改代码

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) {

    }
} );