• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Populating an AlertDialog from an ArrayList<>

mgillespie

Well-Known Member
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:
    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();
    }
 
Back
Top Bottom