弹出对话框中的ListView问题 防止对话框被关掉的代码

本帖最后由 李少卿 于 2012-8-14 10:18 编辑

AlertDialog.Builder.setAdapter方法可以给弹出的对话框设置个ListView

我想点击这个ListView中的某几项给应用做一些设置

但是点击某一项,事件响应后,对话框就关掉了

就是说,点击ListView中的 某一项,效果跟点击下面的确定和取消是一样的

对话框代码如下:

?
代码片段,双击复制
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
final AlertDialog.Builder builder = new AlertDialog.Builder(
                                                        DetailedInterfaceActivity. this );
                                        builder.setTitle( "请选择要下载的内容" );
                                        final boolean [] bol = new boolean [course.length];
                                        final SelectAdapter sa = new SelectAdapter(course, bol);
                                        builder.setAdapter(sa, new DialogInterface.OnClickListener() {
                                                                @Override
                                                                public void onClick(DialogInterface dialog, int which) {
                                                                        bol[which] = !bol[which];
                                                                        sa.notifyDataSetChanged();
                                                                }
                                                        })
                                                        .setPositiveButton( "下载" ,
                                                                        new DialogInterface.OnClickListener() {
                                                                                @SuppressWarnings ( "unchecked" )
                                                                                public void onClick(
//此处是下载处理的代码
                                                                        })
                                                        .setNegativeButton( "取消" ,
                                                                        new DialogInterface.OnClickListener() {
                                                                                public void onClick(
                                                                                                DialogInterface dialog,
                                                                                                int which) {
                                                                                }
                                                                        });
                                        builder.create().show();


BaseAdapter的代码:

?
代码片段,双击复制
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
class SelectAdapter extends BaseAdapter {
                 
                String course[];
                boolean value[];
                CheckBox check[];
                 
                SelectAdapter(String course[], boolean value[]){
                        this .course = course;
                        this .value = value;
                }
 
                @Override
                public int getCount() {
                        check = new CheckBox[course.length];
                        return course.length;
                }
 
                @Override
                public CheckBox getItem( int position) {
                        return check[position];
                }
 
                @Override
                public long getItemId( int position) {
                        return position;
                }
 
                @Override
                public View getView( int position, View convertView, ViewGroup parent) {
                         
                        LayoutInflater laInflater = (LayoutInflater) DetailedInterfaceActivity. this
                                .getSystemService(android.app.Service.LAYOUT_INFLATER_SERVICE);
                        convertView = laInflater.inflate(R.layout.select_layout, null );
                         
                        TextView black = (TextView) convertView.findViewById(R.id.select_black_7531);
                        TextView blue = (TextView) convertView.findViewById(R.id.select_blue_7532);
                        CheckBox check = (CheckBox) convertView.findViewById(R.id.select_checkBox_7533);
                        this .check[position] = check;
                         
                        black.setText(course[position].subSequence( 0 , 5 ));
                        if (- 1 != course[position].indexOf( "###" )){
                                check.setVisibility(View.GONE);
                                blue.setVisibility(View.VISIBLE);
                                blue.setText( "已下载" );
                        } else {
                                check.setChecked(value[position]);
                        }
                         
                        return convertView;
                }
        }


BaseAdapter调用的布局文件:

?
代码片段,双击复制
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<?xml version= "1.0" encoding= "utf-8" ?>
<RelativeLayout
   xmlns:android= "http://schemas.android.com/apk/res/android"
   android:layout_width= "match_parent"
   android:layout_height= "wrap_content"
   android:background= "#ffffff"
   android:orientation= "horizontal" >
   
   <TextView
    android:id= "@+id/select_black_7531"
          android:layout_width= "wrap_content"
          android:layout_height= "wrap_content"
          android:textColor= "@color/black"
          android:layout_marginLeft= "15dip"
          android:layout_marginTop= "15dip"
          android:layout_marginBottom= "5dip"
        android:layout_centerVertical= "true"
          android:textSize= "20sp"
          android:text= "第 1 课" />
           
   <TextView
    android:id= "@+id/select_blue_7532"
          android:layout_width= "wrap_content"
          android:layout_height= "wrap_content"
          android:textColor= "@color/blue"
          android:textSize= "16sp"
        android:layout_alignParentRight= "true"
        android:layout_centerVertical= "true"
        android:layout_marginRight= "10dip"
        android:visibility= "gone"
          android:text= "已下载" />
           
   <CheckBox
    android:id= "@+id/select_checkBox_7533"
        android:layout_width= "wrap_content"
        android:layout_height= "wrap_content"
        android:layout_alignParentRight= "true"
        android:layout_centerVertical= "true"
        android:layout_marginRight= "10dip" />
   
</RelativeLayout>
 

 

 

 

 

已经解决了

防止对话框被关掉的代码

?
代码片段,双击复制
01
02
03
04
05
06
07
                                                                        try {
                                                                                Field field = dialog.getClass().getSuperclass().getDeclaredField( "mShowing" );
                                                                                field.setAccessible( true );
                                                                                field.set(dialog, false );
                                                                        } catch (Exception e) {
                                                                                e.printStackTrace();
                                                                        }


设置可以被关掉的代码:

?
代码片段,双击复制
01
02
03
04
05
06
07
                                                                                        try {
                                                                                                Field field = dialog.getClass().getSuperclass().getDeclaredField( "mShowing" );
                                                                                                field.setAccessible( true );
                                                                                                field.set(dialog, true );
                                                                                        } catch (Exception e) {
                                                                                                e.printStackTrace();
                                                                                        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值