android 往dialog中加入listview,并实现listview中item的点击事件

盛年不重来,一日难再晨,及时当勉励,岁月不待人。这篇文章主要讲述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); //往这个布局中加入listviewfinal 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(); } });


【android 往dialog中加入listview,并实现listview中item的点击事件】再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow


    推荐阅读