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();
}
});
再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow
原文地址:https://www.cnblogs.com/skiwnchhw/p/10349737.html
时间: 2024-10-24 02:19:12