简体   繁体   中英

Java Android - Set items in Alert Box

I am trying to add string items into an alert dialog, but it doesn't let me. This is my Alert Box.

   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 is just getting the first letters of each word.

        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;
               }
           }

Any Ideas? It doesn't let me Set Items to addrev.

Error 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(){})   

Error 2 :

Description Resource    Path    Location    Type
The method size() is undefined for the type String

First point is AlertDialog.Builder.setItems method takes only two parameter :

. First parameter a CharSequence Array

. Second parameter a DialogInterface.OnClickListener

Error 1 : Description Resource Path Location Type The method setItems

Pass abbrev.toCharArray() and listener as second parameter:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM