Numberpicker与数据库中的数组

Numberpicker与数据库中的数组

本文介绍了Numberpicker与数据库中的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想帮助我开发一个带有从firebase获取的列表的数字选择器,此代码现在给我一个错误,如果我删除setMaxValue和MinValue,它只给我一个值。 





如果我留下最小和最大两行错误:





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





我是什么尝试过:



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

}
});
解决方案




I wanted to help me develop a numberpicker with a list that I get from firebase, this code now gives me an error and if I delete "setMaxValue and MinValue" it only brings me a single value.



error if I leave the two lines of min and 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$2.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$1.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)



What I have tried:

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

        }
    } );
解决方案




这篇关于Numberpicker与数据库中的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-28 05:46