I have an application on the market called Memory Tutor that is a simple matching game for kids. One of the features is that you can choose from a number of different tile sets. In the initial version, I had just listed the names of the tile sets in the AlertDialog ListView and didn't have an icon to show what the tiles looked like. I struggled with this for a few hours yesterday (I am not an expert) and couldn't get it to work for the life of me... My problem was that I wasn't using a simple ArrayAdapter but instead had tried using my standard Efficiency Adapter as I would with any other list. I kind of accidentally made it work because I was just trying a bunch of different things. Here is a working example of how to create a custom adapter for an AlertDialog ListView.
First: You need a layout for each row.
Using xml Syntax Highlighting
- <?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>
Parsed in 0.003 seconds, using GeSHi 1.0.8.4
Then you create your ListAdapter:
Using java Syntax Highlighting
- String[] items = {"airplanes", "animals", "cars", "colors", "flowers", "letters", "monsters", "numbers", "shapes", "smileys", "sports", "stars" };
- 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();
- }
- tile = getResources().getDrawable(R.drawable.list_icon); //this is an image from the drawables folder
- holder.title.setText(items[position]);
- holder.icon.setImageDrawable(tile);
- return convertView;
- }
- };
Parsed in 0.034 seconds, using GeSHi 1.0.8.4
Now define your AlertDialog:
Using java Syntax Highlighting
- AlertDialog.Builder builder = new AlertDialog.Builder(MyApp.this);
- builder.setTitle("Pick a tile set");
- 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();
Parsed in 0.031 seconds, using GeSHi 1.0.8.4
In particular, pay attention to how you set the adapter for the AlertDialog. Good Luck!

