17

I have been looking at Android AlertDialog, and its easy enough to use the setItems(...) to add a list of Strings that are to be shown.

However, in most cases you want a list showing nice Strings, but when selecting something from the list you want the actual value and not the String.

I have been unable to find how to do that in an easy and nice way.

Tips? =)

final Button Button1 = (Button) findViewById(R.id.Button1);
Button1.setOnClickListener(new OnClickListener()
{
    @Override
    public void onClick(View v) 
    {
        final CharSequence[] items = { "String 1", "String 2", "String 3" };
        // INstead of a string array, I want something like:
        // ArrayList<CustomObject> test = new ArrayList<CustomObject>(myArray);
        // And the CustomObject has a toString() and also a value. This array should in the best of worlds be the base for the list below =)

        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
        builder.setTitle(LanguageHandler.GetString("Test"));
        builder.setItems(items, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int item) {

                // ***   I want to get the value here!   ***

                Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
            }
        });
        AlertDialog alert = builder.create();
        alert.show();
    }
});
Ted
  • 19,727
  • 35
  • 96
  • 154
  • can you eloborate more? what you want? – user370305 Oct 18 '11 at 17:36
  • Hey, sorry. Didnt see this comment until now. Well, I think its sort of clear on the comment above (the comment in the code that is). I want custom objects to be added to the AlertDialog, where the objects toString()-method should be printed, but when clicking I'd like to get the clicked object to be returned instead... =) – Ted Dec 07 '11 at 12:16
  • Look at my answer, Hope now you get what you want.. Just make custom adapter of your objects and set it to your AlertDialog.. – user370305 Dec 07 '11 at 12:49
  • @Ted If you have time: [Google I/O 2010 - The world of ListView](http://www.youtube.com/watch?v=wDBM6wVEO70). –  May 29 '12 at 02:55

1 Answers1

42

Instead of CharSequence[] items = { "String 1", "String 2", "String 3" }; you can use a Custom Adapter in your Alert Dialog,

Something like,

AlertDialog.Builder builder = new AlertDialog.Builder(MyApp.this);
            builder.setTitle("Select");
            builder.setAdapter(adapter,
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog,
                                int item) {
                            Toast.makeText(MyApp.this, "You selected: " + items[item],Toast.LENGTH_LONG).show();
                            dialog.dismiss();
                        }
                    });
            AlertDialog alert = builder.create();
            alert.show();

Your list_row.xml file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <ImageView
        android:id="@+id/icon"
        android:layout_width="48px"
        android:layout_height="48px"
        android:layout_gravity="left" />

    <TextView
        android:id="@+id/title"
        android:textColor="#0000FF"
        android:text=""
        android:paddingLeft="10dip"
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

And your ListAdapter something like,

String[] items = {"airplanes", "animals", "cars", "colors", "flowers", "letters", "monsters", "numbers", "shapes", "smileys", "sports", "stars" };

// Instead of String[] items, Here you can also use ArrayList for your custom object..

ListAdapter adapter = new ArrayAdapter<String>(
        getApplicationContext(), R.layout.list_row, items) {

    ViewHolder holder;
    Drawable icon;

    class ViewHolder {
        ImageView icon;
        TextView title;
    }

    public View getView(int position, View convertView,
            ViewGroup parent) {
        final LayoutInflater inflater = (LayoutInflater) getApplicationContext()
                .getSystemService(
                        Context.LAYOUT_INFLATER_SERVICE);

        if (convertView == null) {
            convertView = inflater.inflate(
                    R.layout.list_row, null);

            holder = new ViewHolder();
            holder.icon = (ImageView) convertView
                    .findViewById(R.id.icon);
            holder.title = (TextView) convertView
                    .findViewById(R.id.title);
            convertView.setTag(holder);
        } else {
            // view already defined, retrieve view holder
            holder = (ViewHolder) convertView.getTag();
        }       

        Drawable drawable = getResources().getDrawable(R.drawable.list_icon); //this is an image from the drawables folder

        holder.title.setText(items[position]);
        holder.icon.setImageDrawable(drawable);

        return convertView;
    }
};
user370305
  • 108,599
  • 23
  • 164
  • 151