gpt4 book ai didi

java - 在 android 中带有复选框的 AlertDialog

转载 作者:行者123 更新时间:2023-11-30 02:40:09 31 4
gpt4 key购买 nike

我有一个 AlertDialog,它带有几个启用了多项选择的复选框。单击“确定”按钮后,我想将所有选中项目的值存储在字符串变量中。但我只能得到最后检查的项目的值(value)。我希望所有值都存储在一个字符串变量中。

以下是我的 AlertDialog 代码 fragment 。

final CharSequence[] itemsForPattern = { "Patchy", "Wavy Markings", "Stripes", "Bands"};   
String paternSelected = null;
final ArrayList<Object> selectedItems = new ArrayList<>();
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Select the pattern on the wing");

builder.setMultiChoiceItems(itemsForPattern, null, new DialogInterface.OnMultiChoiceClickListener() {

@Override
public void onClick(DialogInterface dialog, int indexSelected, boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked){
selectedItems.add(indexSelected);
}
else if(selectedItems.contains(indexSelected)){
//This line is giving arrayIndexOutOfBounds exception after unchecking a checkeditem
selectedItems.remove(indexSelected);
}}
})
.setPositiveButton("OK", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub

//Here I'm successfully able to get the checked item values
for(int i= 0; i < selectedItems.size(); i++){
paternSelected = (String) itemsForPattern[(int) selectedItems.get(i)];
tv_Choosen.append("(" + paternSelected + ")");
}

if(patternCode.equalsIgnoreCase("hind-wing")){
// Here I'm trying to save all the values in one single String variable for which
// I'm getting only the last checked value here
String S2DB_hw_pattern = paternSelected;
}
else
String S2DB_fw_pattern = paternSelected;
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub

}
});

dialog = builder.create();
dialog.show();

错误如下

 E/AndroidRuntime(1915): FATAL EXCEPTION: main
E/AndroidRuntime(1915): java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1
09-17 17:49:30.586: E/AndroidRuntime(1915): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
09-17 17:49:30.586: E/AndroidRuntime(1915): at java.util.ArrayList.remove(ArrayList.java:399)
09-17 17:49:30.586: E/AndroidRuntime(1915): at com.example.projectb.main.ImageMapTestActivity$3.onClick(ImageMapTestActivity.java:532)
09-17 17:49:30.586: E/AndroidRuntime(1915): at com.android.internal.app.AlertController$AlertParams$4.onItemClick(AlertController.java:953)
09-17 17:49:30.586: E/AndroidRuntime(1915): at android.widget.AdapterView.performItemClick(AdapterView.java:298)
09-17 17:49:30.586: E/AndroidRuntime(1915): at android.widget.AbsListView.performItemClick(AbsListView.java:1100)
09-17 17:49:30.586: E/AndroidRuntime(1915): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2749)
09-17 17:49:30.586: E/AndroidRuntime(1915): at android.widget.AbsListView$1.run(AbsListView.java:3423)
09-17 17:49:30.586: E/AndroidRuntime(1915): at android.os.Handler.handleCallback(Handler.java:725)
09-17 17:49:30.586: E/AndroidRuntime(1915): at android.os.Handler.dispatchMessage(Handler.java:92)
09-17 17:49:30.586: E/AndroidRuntime(1915): at android.os.Looper.loop(Looper.java:137)
09-17 17:49:30.586: E/AndroidRuntime(1915): at android.app.ActivityThread.main(ActivityThread.java:5039)
09-17 17:49:30.586: E/AndroidRuntime(1915): at java.lang.reflect.Method.invokeNative(Native Method)
09-17 17:49:30.586: E/AndroidRuntime(1915): at java.lang.reflect.Method.invoke(Method.java:511)
09-17 17:49:30.586: E/AndroidRuntime(1915): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
09-17 17:49:30.586: E/AndroidRuntime(1915): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
09-17 17:49:30.586: E/AndroidRuntime(1915): at dalvik.system.NativeStart.main(Native Method)

最佳答案

if(isChecked){
selectedItems.add(indexSelected);
}
else if(selectedItems.contains(indexSelected)){
//This line is giving arrayIndexOutOfBounds exception after unchecking a checkeditem
selectedItems.remove(selectedItems.indexOf(indexSelected)); //<--Add this
}}

关于java - 在 android 中带有复选框的 AlertDialog,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25896990/

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