Android:在AlertDialog内的多选列表视图中选择项目
我是android开发的新手,正在为如何在alertdialog托管的listview中选择某些项目而苦苦挣扎。在下面的代码中,lv.setItemChecked不起作用,因为listview尚未生成,所以我想知道是否有任何listview或AlertDialog事件确认该视图已生成Android:在AlertDialog内的多选列表视图中选择项目,android,listview,android-alertdialog,selecteditem,Android,Listview,Android Alertdialog,Selecteditem,我是android开发的新手,正在为如何在alertdialog托管的listview中选择某些项目而苦苦挣扎。在下面的代码中,lv.setItemChecked不起作用,因为listview尚未生成,所以我想知道是否有任何listview或AlertDialog事件确认该视图已生成 String [] values = {"a","b","c"}; ArrayAdapter<String> adp = new ArrayAdapter<String>(this,and
String [] values = {"a","b","c"};
ArrayAdapter<String> adp = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_multiple_choice, values);
ListView lv = new ListView(this);
lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
lv.setOnItemClickListener(this);
lv.setAdapter(adp);
AlertDialog.Builder bldr = new AlertDialog.Builder(this);
bldr.setTitle("Select");
bldr.setView(lv);
bldr.setPositiveButton("Done",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
handleDone();
}
});
bldr.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
handleCancel();
}
});
final Dialog dlg = bldr.create();
dlg.show();
String[]值={“a”、“b”、“c”};
ArrayAdapter adp=新的ArrayAdapter(这是android.R.layout.simple\u list\u item\u多项选择,值);
ListView lv=新的ListView(本);
lv.setChoiceMode(ListView.CHOICE\u MODE\u MULTIPLE);
lv.setOnItemClickListener(本);
低压设置适配器(adp);
AlertDialog.Builder bldr=新建AlertDialog.Builder(此);
bldr.setTitle(“选择”);
bldr.setView(lv);
bldr.setPositiveButton(“完成”,
新建DialogInterface.OnClickListener(){
@凌驾
public void onClick(DialogInterface dialog,int which){
handleDone();
}
});
bldr.setNegativeButton(“取消”,
新建DialogInterface.OnClickListener(){
@凌驾
public void onClick(DialogInterface dialog,int which){
handleCancel();
}
});
最终对话框dlg=bldr.create();
dlg.show();
没关系,我知道了。在lv.setAdapter()调用之后,我正在调用lv.setItemChecked(0,true)。当我在dlg.show()之后移动它时,它就像一个符咒
public class DialogoSeleccion extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final String[] items = {"Español", "Inglés", "Francés"};
AlertDialog.Builder builder =
new AlertDialog.Builder(getActivity());
builder.setTitle("Selección")
.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
Log.i("Dialogos", "Opción elegida: " + items[item]);
}
});
return builder.create();
}
}
你会得到这样的结果:
如果要记住或显示最后选定的项,只需更改set setSingleChoiceItems()或setMultiChiceItems()的setItems方法。使用setSingleChoiceItems()很简单,只需传递其他参数(集合选择的索引,如果不想设置,则传递-1):
使用上面的代码片段,您将得到如下内容
如果您想要一个多选项,您应该更改方法,第二个参数现在不是整数,应该是一个布尔数组,通过这种方式您可以设置id任何选项是否启用:
builder.setTitle("Selección")
.setMultiChoiceItems(items, null,
new DialogInterface.OnMultiChoiceClickListener() {
public void onClick(DialogInterface dialog, int item, boolean isChecked) {
Log.i("Dialogos", "Opción elegida: " + items[item]);
}
});
结果将是:
调用任何示例的方法如下:
FragmentManager fragmentManager = getSupportFragmentManager();
DialogoSeleccion dialogo = new DialogoSeleccion();
dialogo.show(fragmentManager, "tagSeleccion");
如果您懂西班牙语,本指南将对您有所帮助:或者在这里获取完整的示例,在中,这是一个简单的复制过去的解决方案。用ArrayList替换
列表
变量,就可以开始了。希望这有帮助
final CharSequence[] dialogList = list.toArray(new CharSequence[list.size()]);
final android.app.AlertDialog.Builder builderDialog = new android.app.AlertDialog.Builder(mContext);
builderDialog.setTitle("Select Item");
int count = dialogList.length;
boolean[] is_checked = new boolean[count];
// Creating multiple selection by using setMutliChoiceItem method
builderDialog.setMultiChoiceItems(dialogList, is_checked,
new DialogInterface.OnMultiChoiceClickListener() {
public void onClick(DialogInterface dialog,
int whichButton, boolean isChecked) {
}
});
builderDialog.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ListView list = ((android.app.AlertDialog) dialog).getListView();
//ListView has boolean array like {1=true, 3=true}, that shows checked items
}
});
builderDialog.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
((TextView) myFilesActivity.findViewById(R.id.text)).setText("Click here to open Dialog");
}
});
android.app.AlertDialog alert = builderDialog.create();
alert.show();
我找到了正确的解决方案:
public void alertMultipleChoiceItems(){
final CharSequence[] dialogList = Symbollist.toArray(new CharSequence[Symbollist.size()]);
HashSet<String> uniqueValues = new HashSet<>(Symbollist);
Symbollist.clear();
for (String value : uniqueValues) {
//... //Do something
Symbollist.add(value);
}
AlertDialog.Builder builder = new AlertDialog.Builder(AddPackageStep3.this);
selectedItems = new ArrayList<Integer>();
// set the dialog title
boolean[] itemChecked = new boolean[selectedItems.size()];
builder.setMultiChoiceItems(dialogList,null, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
if (isChecked) {
// if the user checked the item, add it to the selected items
selectedItems.add(which);
}
else if (selectedItems.contains(which)) {
// else if the item is already in the array, remove it
selectedItems.remove(Integer.valueOf(which));
}
// you can also add other codes here,
// for example a tool tip that gives user an idea of what he is selecting
// showToast("Just an example description.");
}
})
// Set the action buttons
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
// user clicked OK, so save the mSelectedItems results somewhere
// here we are trying to retrieve the selected items indices
String selectedIndex = "";
for(Integer i : selectedItems){
selectedIndex += i + ", ";
}
//showToast("Selected index: " + selectedIndex);
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
// removes the AlertDialog in the screen
}
})
.show();
}
public void alertmultipleechoiceitems(){
final CharSequence[]dialogList=Symbollist.toArray(新的CharSequence[Symbollist.size());
HashSet uniqueValues=新的HashSet(符号列表);
Symbollist.clear();
for(字符串值:唯一值){
//…做点什么
符号列表。添加(值);
}
AlertDialog.Builder=新建AlertDialog.Builder(AddPackageStep3.this);
selectedItems=newarraylist();
//设置对话框标题
boolean[]itemChecked=new boolean[selectedItems.size()];
setMultiChoiceItems(dialogList,null,new DialogInterface.OnMultiChoiceClickListener()){
@凌驾
public void onClick(DialogInterface dialog,int,其中布尔值被选中){
如果(已检查){
//如果用户选中了该项目,请将其添加到所选项目中
选择editems.add(哪个);
}
else if(selectedItems.contains(which)){
//否则,如果该项已在数组中,请将其删除
选择editems.remove(Integer.valueOf(which));
}
//您也可以在此处添加其他代码,
//例如,一个工具提示,让用户知道他在选择什么
//showToast(“只是一个示例描述”);
}
})
//设置动作按钮
.setPositiveButton(“确定”,新的DialogInterface.OnClickListener(){
@凌驾
public void onClick(DialogInterface对话框,int-id){
//用户单击“确定”,因此将mSelectedItems结果保存到某个位置
//在这里,我们试图检索所选项目索引
字符串selectedIndex=“”;
对于(整数i:selectedItems){
选择指数+=i+“,”;
}
//showToast(“选定索引:+selectedIndex”);
}
})
.setNegativeButton(“取消”,新建DialogInterface.OnClickListener()){
@凌驾
public void onClick(DialogInterface对话框,int-id){
//删除屏幕中的AlertDialog
}
})
.show();
}
public void alertMultipleChoiceItems(){
final CharSequence[] dialogList = Symbollist.toArray(new CharSequence[Symbollist.size()]);
HashSet<String> uniqueValues = new HashSet<>(Symbollist);
Symbollist.clear();
for (String value : uniqueValues) {
//... //Do something
Symbollist.add(value);
}
AlertDialog.Builder builder = new AlertDialog.Builder(AddPackageStep3.this);
selectedItems = new ArrayList<Integer>();
// set the dialog title
boolean[] itemChecked = new boolean[selectedItems.size()];
builder.setMultiChoiceItems(dialogList,null, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
if (isChecked) {
// if the user checked the item, add it to the selected items
selectedItems.add(which);
}
else if (selectedItems.contains(which)) {
// else if the item is already in the array, remove it
selectedItems.remove(Integer.valueOf(which));
}
// you can also add other codes here,
// for example a tool tip that gives user an idea of what he is selecting
// showToast("Just an example description.");
}
})
// Set the action buttons
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
// user clicked OK, so save the mSelectedItems results somewhere
// here we are trying to retrieve the selected items indices
String selectedIndex = "";
for(Integer i : selectedItems){
selectedIndex += i + ", ";
}
//showToast("Selected index: " + selectedIndex);
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
// removes the AlertDialog in the screen
}
})
.show();
}