LinearLayout linearLayoutMain = new LinearLayout(this);//自定义一个布局文件 linearLayoutMain.setLayoutParams(new LayoutParams( LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)); ListView listView = new ListView(this);//this为获取当前的上下文 listView.setFadingEdgeLength(0); List<Map<String, String>> nameList = new ArrayList<Map<String, String>>();//建立一个数组存储listview上显示的数据 for (int m = 0; m < initData.size(); m++) {//initData为一个list类型的数据源 Map<String, String> nameMap = new HashMap<String, String>(); nameMap.put("name", initData.get(m).get(TagItem.NAME).toString()); nameList.add(nameMap); } SimpleAdapter adapter = new SimpleAdapter(UploadPhotoActivity.this, nameList, R.layout.item_uploadphoto_select_name, new String[] { "name" }, new int[] { R.id.tv_uploadphoto_select_name_item }); listView.setAdapter(adapter); linearLayoutMain.addView(listView);//往这个布局中加入listview final AlertDialog dialog = new AlertDialog.Builder(this) .setTitle("选择患者姓名").setView(linearLayoutMain)//在这里把写好的这个listview的布局加载dialog中 .setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub dialog.cancel(); } }).create(); dialog.setCanceledOnTouchOutside(false);//使除了dialog以外的地方不能被点击 dialog.show(); listView.setOnItemClickListener(new OnItemClickListener() {//响应listview中的item的点击事件 @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub TextView tv = (TextView) arg1 .findViewById(R.id.tv_uploadphoto_select_name_item);//取得每条item中的textview控件 et_name.setText(tv.getText().toString()); dialog.cancel(); } });