Java Android在警报框中设置项目
我试图将字符串项添加到警报对话框中,但它不允许我这样做。 这是我的警报箱
AlertDialog.Builder builder2 = new AlertDialog.Builder(this);
builder2.setTitle("Make a choice from the list:");
builder2.setCancelable(false);
builder2.setItems(abbrev.toCharArray(),(new String[abbrev.size()]), new DialogInterface.OnClickListener(){
//builder2.setItems(matches.toArray(new String[matches.size()]), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
Toast.makeText(getApplicationContext(), "Selection: " + item, Toast.LENGTH_SHORT).show();
abbrev只是得到每个单词的第一个字母
String[] result = matches.toString().split("\\s+");
// The string we'll create
String abbrev = "";
// Loop over the results from the string splitting
for (int i = 0; i < result.length; i++){
// Grab the first character of this entry
char c = result[i].charAt(0);
// If its a number, add the whole number
if (c >= '0' && c <= '9'){
abbrev += result[i];
}
// If its not a number, just append the character
else{
abbrev += c;
}
}
有什么想法吗? 它不允许我将项目设置为addrev
错误1:
Description Resource Path Location Type
The method setItems(int, DialogInterface.OnClickListener) in the type AlertDialog.Builder is not applicable for the arguments (char[], String[], new DialogInterface.OnClickListener(){})
错误2:
Description Resource Path Location Type
The method size() is undefined for the type String
# 1 楼答案
第一点是AlertDialog.Builder.setItems方法只接受两个参数:
。第一个参数是
CharSequence
数组。第二个参数a
DialogInterface.OnClickListener
将
abbrev.toCharArray()
和侦听器作为第二个参数传递: