Populating an AlertDialog from an ArrayList<>

Put your problem here if it does not fit any of the other categories.

Populating an AlertDialog from an ArrayList<>

Postby mgillespie » Wed Jun 02, 2010 12:26 pm

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: Select all
    class Meat
    {
   public Meat(String string, int i)
   {
       Name = string;
       Code = i;
   }

   public String Name;
   public int Code;
    }

    private ArrayList<Meat> Meats = new ArrayList<Meat>();


              Meats.add(new Meat("Sausages", 33));
   Meats.add(new Meat("Hamburgers", 44));
   Meats.add(new Meat("PorkChop", 55));


   void SelectMeat()
    {
   AlertDialog.Builder builder = new AlertDialog.Builder(this);
   builder.setTitle("Select Your Meat!");
   builder.setItems(items, new DialogInterface.OnClickListener()
   {
       public void onClick(DialogInterface dialog, int item)
       {
      Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
       }
   });
   AlertDialog alert = builder.create();
   alert.show();
    }

mgillespie
Freshman
Freshman
 
Posts: 7
Joined: Sun May 30, 2010 5:10 pm

Top

Re: Populating an AlertDialog from an ArrayList<>

Postby anarche » Thu Jun 03, 2010 12:32 am

Extend either the AlertDialog(ue) or -preferable - extend Dialog.

Write your own .xml layout including a ListView, which you populate with an ArrayAdapter<?>. The adapter you can populate from the ArrayList<?>.

There are plenty of threads on this forum explaining populating a ListView
anarche
Master Developer
Master Developer
 
Posts: 369
Joined: Sun Apr 11, 2010 5:21 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Alexa [Bot], MSNbot Media and 15 guests