android 往dialog中加入listview,并实现listview中item的点击事件
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();
}
});
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。