Java Android - 在Alert Box中设置项目

时间:2015-02-20 19:02:42

标签: java android alertdialog

我正在尝试将字符串项添加到警报对话框中,但它不允许我。 这是我的警报框。

   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 个答案:

答案 0 :(得分:0)

第一点是AlertDialog.Builder.setItems方法只需要两个参数:

。第一个参数a CharSequence数组

。第二个参数a DialogInterface.OnClickListener

  

错误1:说明资源路径位置类型方法setItems

abbrev.toCharArray()和侦听器作为第二个参数传递:

builder2.setItems(abbrev.toCharArray(), new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog2, int item) {
               //...your code here
        });
 }