我是Android开发的新手,并且正在努力选择如何在由alertdialog托管的列表视图中选择某些项目。在下面的代码中,lv.setItemChecked不起作用,因为listview尚未生成,所以我想知道是否有任何ListView或AlertDialog事件确认已生成视图。
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();
没关系,我明白了。我在lv.setAdapter()调用之后立即调用lv.setItemChecked(0,true)。一旦我在dlg.show()之后移动它,它就像一个魅力。
答案 0 :(得分:15)
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):
builder.setTitle("Selección")
.setSingleChoiceItems(items, -1,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
Log.i("Dialogos", "Opción elegida: " + items[item]);
}
});
使用上面的代码片段,您将拥有类似的内容
如果你想要一个multichoose,你应该改变方法,第二个参数现在不是一个整数,应该是一个布尔数组,通过这种方式你将设置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");
如果您了解西班牙语,本指南将为您提供帮助:Complete guide for AlertDialogs或在GitHub
中获取完整示例答案 1 :(得分:1)
这是简单的复制过去解决方案。用你的ArrayList替换awk
变量,你就可以了。希望这会有所帮助。
list
答案 2 :(得分:0)
我找到了正确的解决方案:
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();
}