ExpandableListView 添加其他控件

可折叠式的list控件使用系统的SimpleExpandableListAdap ter 在每一项里添加图片时老是报错,
在网上查了些资料大部分提供的解决方案是自定义一个Adapter 继承BaseExpandableListAdapte r的方式,感觉忒麻烦了,于是就根据错误信息看了看系统SimpleExpandableListAdap ter的源码  关键代码如下:
SimpleExpandableListAdap ter .java

private void bindView(View view, Map<String, ?> data, String[] from, int[] to) {
              int len = to.length;

              for (int i = 0; i < len; i++) {
                    View viewItem=view.findViewById(to[i]);
                          TextView v = (TextView)view.findViewById(to[i]);
                              if (v != null) {
                                      v.setText((String)data.get(from[i]));
                              }
                    }
                     
              }
      }
果然,他将每一项都看成一个TextView 了,难怪不能添加图片,我们只需添加小小判断就可满足需求
修改后如下:
private void bindView(View view, Map<String, ?> data, String[] from, int[] to) {
              int len = to.length;

              for (int i = 0; i < len; i++) {
                    View viewItem=view.findViewById(to[i]);
                    if(viewItem instanceof TextView){
                          TextView v = (TextView)view.findViewById(to[i]);
                              if (v != null) {
                                      v.setText((String)data.get(from[i]));
                              }
                    }else if(viewItem instanceof ImageView){
                          ImageView imgView =(ImageView) view.findViewById(to[i]);
                          if(imgView!=null){
                          imgView.setImageResource(new Integer(data.get(from[i]).toString()));
                          }
                             
              }
      }
我这里只加了ImageView 的判断,已经满足了我的需求,当然如果需要别的控件的话,可以再加其他判断。
这样比自定义适配器省事多了
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值