Populating an AlertDialog from an ArrayList<>


Last Updated:

  1. mgillespie

    mgillespie Well-Known Member This Topic's Starter

    Joined:
    Mar 5, 2010
    Messages:
    243
    Likes Received:
    13
    Hi, can someone give a newbie a bit of assistance. I am more used to Windows development (C/C++/C#) than Java and Android. I thought I'd take a stab at a Android app. I want to create an AlertDialog populated from a ArrayList<MyType> where MyType is a class of string and a int. (I want to display the string, but get back the int). I'm sorta stuck working out how to do it.

    Snippets of relevent code:

    Code (Text):
    1.  
    2.     class Meat
    3.     {
    4.     public Meat(String string, int i)
    5.     {
    6.         Name = string;
    7.         Code = i;
    8.     }
    9.  
    10.     public String Name;
    11.     public int Code;
    12.     }
    13.  
    14.     private ArrayList<Meat> Meats = new ArrayList<Meat>();
    15.  
    16.  
    17.               Meats.add(new Meat("Sausages", 33));
    18.     Meats.add(new Meat("Hamburgers", 44));
    19.     Meats.add(new Meat("PorkChop", 55));
    20.  
    21.  
    22.    void SelectMeat()
    23.     {
    24.     AlertDialog.Builder builder = new AlertDialog.Builder(this);
    25.     builder.setTitle("Select Your Meat!");
    26.     builder.setItems(items, new DialogInterface.OnClickListener()
    27.     {
    28.         public void onClick(DialogInterface dialog, int item)
    29.         {
    30.         Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
    31.         }
    32.     });
    33.     AlertDialog alert = builder.create();
    34.     alert.show();
    35.     }
    36.  
    37.  
     

    Advertisement

Share This Page

Loading...