MainActivity.java
package com.example.dialoglisttest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
public class MainActivity extends Activity {
final int LIST_DIALOG = 0x001;
// 定义一个资源数组,分别为图片和名称
public int[] images = { R.drawable.bird, R.drawable.dolphin,
R.drawable.panda };
public String[] names = { "画眉鸟", "海豚", "熊猫" };
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnShow = (Button) findViewById(R.id.btnSelect);
btnShow.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// 显示Dialog
showDialog(LIST_DIALOG);
}
});
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case LIST_DIALOG:
// 创建一个builder实例
Builder builder = new AlertDialog.Builder(MainActivity.this);
// 设置对话框的图标和名称
builder.setIcon(R.drawable.tool);
builder.setTitle("动物列表");
// 创建一个List集合,用来存放列表项
List<Map<String, Object>> listitems = new ArrayList<Map<String, Object>>();
// 使用for循环将数据添加到listitems集合中去
for (int i = 0; i < images.length; i++) {
Map<String, Object> listitem = new HashMap<String, Object>();
listitem.put("image", images[i]);
listitem.put("names", names[i]);
listitems.add(listitem);
}
// 创建SimpleAdapter
SimpleAdapter adapter = new SimpleAdapter(this, listitems,
R.layout.row, new String[] { "image", "names" }, new int[] {
R.id.image, R.id.name });
/**
* 设置一个列表项
*/
builder.setAdapter(adapter, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
TextView textView = (TextView) findViewById(R.id.text);
ImageView imageView=(ImageView) findViewById(R.id.imageShow);
textView.setText("你所选择的喜欢的动物是:" + names[which]);
imageView.setImageResource(images[which]);
}
});
// 创建builder
builder.create();
// 显示builder
builder.show();
}
return null;
}
}
row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ImageView
android:id="@+id/image"
android:layout_width="80dp"
android:layout_height="80dp"
android:paddingLeft="10pt" />
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10pt" />
</LinearLayout>
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
android:orientation="vertical" >
<TextView
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:hint="显示你所选择的动物"
android:text=""
android:textSize="20dp" />
<ImageView
android:id="@+id/imageShow"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btnSelect"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="选择你所喜欢的的动物" />
</LinearLayout>