Android 设置多选项目
Android setMultiChoiceItems
当我单击一个按钮时,会弹出一个对话框,其中包含多个 selection 复选框。当我 select 并单击“确定”时,它会弹出另一个带有 selected item.If 的对话框=] 和 select 并单击确定,它会附加上一个 selection 的结果。我不想附加结果。我不理解或犯错了什么?代码在这里
@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;
}
试试这个,
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();
当我单击一个按钮时,会弹出一个对话框,其中包含多个 selection 复选框。当我 select 并单击“确定”时,它会弹出另一个带有 selected item.If 的对话框=] 和 select 并单击确定,它会附加上一个 selection 的结果。我不想附加结果。我不理解或犯错了什么?代码在这里
@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;
}
试试这个,
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();