Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6923753
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:34:36+00:00 2026-05-27T10:34:36+00:00

I have been looking at Android AlertDialog, and its easy enough to use the

  • 0

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();
    }
});
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T10:34:36+00:00Added an answer on May 27, 2026 at 10:34 am

    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;
        }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been looking for a easy way to add borders to custom made
Have been looking at the MVC storefront and see that IQueryable is returned from
I have been looking into IKVMing Apache's FOP project to use with our .NET
I have been looking at Android's javax.net.ssl package, but am still confused as to
I have been looking into MeeGo, maemo, Android architecture. They all have Linux Kernel,
I have been looking over some of the android source, and the examples given
I have been looking for that so much and I haven't found the solution.
I have been attempting to use an android.preference.DialogPreference inflated from XML, but the documentation
I have been looking around on how to customize Android TabWidget using XML with
I have been looking around for a good Android AR toolkit to play with.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.