弹出列表对话框,选择列表项后返回结果并消失
来源:互联网 发布:Linux安装telnet yum 编辑:程序博客网 时间:2024/05/21 09:41
最近的开发过程中,有选择时间段的需求,需要弹出一个列表对话框,用户选择完列表项之后返回用户选择结果,当前列表项消失。
首先,在res/layout文件夹定义一个列表项对应的布局文件以便加载:
<?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="vertical" > <ListView android:id="@+id/list" android:layout_width="match_parent" android:layout_height="wrap_content" android:entries="@array/time_zone" android:divider="#cccccc" android:dividerHeight="1px" /> </LinearLayout>
单击按钮,弹出该布局:
Button time = (Button) findViewById(R.id.time);time.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(final View v) {//LayoutInflater inflater = LayoutInflater.from(this);LinearLayout ll = (LinearLayout) getLayoutInflater().inflate(R.layout.time, null);final ListView lv = (ListView) ll.findViewById(R.id.list);AlertDialog.Builder builder = new AlertDialog.Builder(TabActivity_book.this)//.setSingleChoiceItems(items, 0, new DialogInterface.OnClickListener() {////@Override//public void onClick(DialogInterface dialog, int which) {//((Button)v).setText(items[which]);////}//});.setView(ll);dialog = builder.create();dialog.show();lv.setOnItemClickListener(new OnItemClickListener() {@Overridepublic void onItemClick(AdapterView<?> parent, View view,int position, long id) {time.setText(lv.getItemAtPosition(position).toString());//ll.removeView(lv);dialog.dismiss();}});}});
注:如果把
LinearLayout ll = (LinearLayout) getLayoutInflater().inflate(R.layout.time, null);final ListView lv = (ListView) ll.findViewById(R.id.list);
这2段代码放在监听器方法的外面,则在单击按钮选择时间后再次单击,会报异常:
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
原因是ListView既属于LinearLayout,而LinearLayout又是由Dialog弹出的——如果把加载页面和相应的列表项放在外面时,在第一次弹出列表框时,已经kill掉了dialog,这就间接kill掉了属于他的LinearLayout。当用户再次单击按钮试图再次加载该布局文件时,LinearLayout已经在上一次(第一次)被kill掉了(因为他是放在外面的)。所以报错!
解决办法则是把这2行代码放在监听器方法里面,每单击一次就重新创建一次Linearlayout,即加载布局文件,在用户选择列表项后,dismiss该弹出的对话框。
0 0
- 弹出列表对话框,选择列表项后返回结果并消失
- 对话框弹出后,点其他地方不消失
- android弹出带图标的列表对话框
- phonegap 长按列表弹出对话框
- 长按listview弹出选项列表对话框
- 显示选择省份的三种列表对话框-列表对话框,单选列表对话框,多选列表对话框
- 百度地图 根据关键字搜索 并返回信息列表并根据选择位置信息得到经纬度
- 函数参数列表返回结果示例
- CASE 计算条件列表并返回多个可能结果表达式之一
- Spring3MVC提交弹出窗口表单后,自动返回父窗口的列表页面
- debug案例---下拉列表框选择查询字段后,没有显示结果。
- qt弹出对话框选择图片并显示
- qt弹出对话框选择视频并播放
- 弹出列表框,响应选择的数据
- ajax实现点击弹出选择列表
- 移动端弹出列表选择框
- 用wx.SingleChoiceDialog创建选择列表对话框
- android中单选列表对话框-选择个人特长
- 对视图进行 DML 操作
- 多项式Polynomial
- 使用ListView保存历史输入记录
- 改进后的汉字截取处理程序(含测试用例)
- JSP“抓”网页代码的程序
- 弹出列表对话框,选择列表项后返回结果并消失
- Java实现数据统计的常用算法
- 性能调优攻略
- 启动其他Activity并保存结果到指定组件上
- OFBIZ开源电子商务学习心得
- matlab error:Attempt to execute SCRIPT as a function
- 一.spring中@InitBinder和WebBindingInitializer的使用
- 一种利用线程池进行for循环处理的思想
- 比ant更强大的工具maven2之自动生成工程