gpt4 book ai didi

java - Android 对话框 setMultiChoiceItems/arraylist 删除不工作

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

这是我的代码:

public class MainActivity extends AppCompatActivity  {

ArrayList<String> nameItems, selectedItems;
ArrayList<Integer> numCheckedArray;
TextView mTextView;
Button mButton;
String[] namesStringArray;
boolean[] checkedItems;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

nameItems = new ArrayList();

selectedItems = new ArrayList<>();
numCheckedArray = new ArrayList<>();


nameItems.add("Test 1");
nameItems.add("Test 2");
nameItems.add("Test 3");
nameItems.add("Test 4");

namesStringArray = new String[nameItems.size()];

for (int i =0; i< nameItems.size(); i++){
namesStringArray[i] = nameItems.get(i);
}

checkedItems = new boolean[nameItems.size()];



mButton =(Button)findViewById(R.id.testButton);
mTextView =(TextView)findViewById(R.id.testTextView);

mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Select Your Items");
builder.setMultiChoiceItems(namesStringArray, checkedItems, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {


if (isChecked){
//add the position that the user clicked on
numCheckedArray.add(which);


}else if (numCheckedArray.contains(which)) {
//remove the position that the user clicked on
numCheckedArray.remove(Integer.valueOf(which));

}
}
});

builder.setCancelable(false);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {



//add selected items into selected item array still need to figure out how to remove it from the array
for (int i = 0; i < numCheckedArray.size(); i++){

//if item is already in array and is still checked don't do anything
if (selectedItems.contains(nameItems.get(numCheckedArray.get(i))) && checkedItems[numCheckedArray.get(i)] == true){
Log.d("test", "do nothing is called");

}

//if item is already in array and is not checked remove from array
else if (selectedItems.contains(nameItems.get(numCheckedArray.get(i))) && checkedItems[numCheckedArray.get(i)] == false){
Log.d("test", "Remove is called");
selectedItems.remove(nameItems.get(numCheckedArray.get(i)));

}

//add item to array
else {
Log.d("test", "add is called");
selectedItems.add(nameItems.get(numCheckedArray.get(i)));
}
}



//used to see the values of the arrays

for (int i = 0; i < checkedItems.length; i++){
Log.d("test", String.valueOf(checkedItems[i]));
}
for (int i = 0; i < selectedItems.size(); i++){
Log.d("test", selectedItems.get(i));
}
}
});

builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {

}
});

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

我想通过我的计划实现 3 个目标。它们是:

  • 当一个项目被选中但不在 selectedItems arraylist 中时,当我单击 ok 时将其添加到 arraylist(这部分工作正常)。

  • 当一个项目被选中并且已经在 selectedItems arraylist 中时,当我点击 ok 时什么也不做(这部分也可以正常工作)。

  • 当一个项目未被选中并且已经在 selectedItems arraylist 中时,当我单击 ok 时将其从 arraylist 中删除(这部分不起作用并且让我抓狂)。

我的问题是,为什么永远不会调用以下代码行:

Log.d("test", "Remove is called");  
selectedItems.remove(nameItems.get(numCheckedArray.get(i)));

我不明白为什么逻辑在这里不起作用。

谢谢!

最佳答案

因为您在取消选中时删除未选中的项目(甚至在您按确定之前)

在这个区 block 中

else if (numCheckedArray.contains(which)) {
//remove the position that the user clicked on
numCheckedArray.remove(Integer.valueOf(which));
}

所以当您检查此 checkedItems[numCheckedArray.get(i)] 时,所有项目都是真实的,因为您删除了未检查的项目

如果要执行 Log.d("test", "Remove is called"); 注释else部分 numCheckedArray.remove(Integer.valueOf(which));

关于java - Android 对话框 setMultiChoiceItems/arraylist 删除不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41625510/

31 4 0