单击按钮时会弹出一个带有多个选择复选框的对话框。当我选择并单击确定时,它会弹出另一个带有所选项目的对话框。如果我通过单击对话框外部关闭对话框并再次按下按钮以显示要选择的对话框并选择并单击确定它会附加先前选择的结果。我不想追加结果。我不理解或犯错的是什么?代码在这里
@Override
public View onCreateView(final LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_tab2, container, false);
Button button = (Button) view.findViewById(R.id.displayBox);
final List<String> selectedItems = new ArrayList<String>();
final String[] itemList = {"Item1", "Item2", "Item3", "Item1", "Item2", "Item3", "Item1", "Item2", "Item3", "Item1", "Item2", "Item3"};
final AlertDialog.Builder builder = new AlertDialog.Builder(view.getContext());
builder.setTitle("Multi Select");
builder.setMultiChoiceItems(itemList, null, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
if (isChecked) {
selectedItems.add(itemList[which].toString());
}
}
}).setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ListView listView = new ListView(view.getContext());
listView.setAdapter(new ArrayAdapter<String>(view.getContext(), android.R.layout.simple_list_item_1, selectedItems));
Dialog dialog1 = new Dialog(view.getContext());
dialog1.setTitle("Selected Informations");
dialog1.setContentView(listView);
dialog1.show();
}
});
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
builder.show();
}
});
return view;
}
答案 0 :(得分:0)
试试这个,
ListView listView = new ListView(view.getContext());
listView.setAdapter(new ArrayAdapter<String>(view.getContext(), android.R.layout.simple_list_item_1, selectedItems));
//清除列表视图
selectedItems.clear();
Dialog dialog1 = new Dialog(view.getContext());
dialog1.setTitle("Selected Informations");
dialog1.setContentView(listView);
dialog1.show();