gpt4 book ai didi

java - NumberPicker 与数据库中的数组

转载 作者:太空宇宙 更新时间:2023-11-04 09:43:49 27 4
gpt4 key购买 nike

我想帮助我开发一个带有从 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) {

}
} );

如果我留下 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$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)

最佳答案

您正在 CBSList 完全填充之前设置 setMaxValuesetMinValue

像这样更改你的代码

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

}
} );

关于java - NumberPicker 与数据库中的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55656558/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com