android expandlistview group imageview报错的解决办法

在expandListView的group中如果包含imageview发现,报错。提示无法将imageview转换成TextView.查看源码发现,SimpleExpandableListAdapter中有如下方法:

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

        for (int i = 0; i < len; i++) {
            TextView v = (TextView)view.findViewById(to[i]);
            if (v != null) {
                v.setText((String)data.get(from[i]));
            }
        }
    }

可以看出bindView把所有view都当textView来处理了。真是完全没有老大哥simpleAdapter的防范,可以自动判断类型绑定。网上普遍给出的方法是重写bindView方法:

public class MyExpandableListAdapter extends BaseExpandableListAdapter{
        private void bindView(View view, Map<String, ?> data, String[] from, int[] to) {
                int len = to.length;
                boolean isBound = false;
                for (int i = 0; i < len; i++) {
                   final View v = view.findViewById(to[i]);
                 if (v!=null) {
                final Object _data = data.get(from[i]);
                String text = _data == null ? "" : data.toString();
                if (text == null) {
                    text = "";
                }
                          if (mViewBinder != null) {//如果Binder不为空,使用Binder进行处理
                                        isBound = mViewBinder.setViewValue(v, data.get(from[i]), text);
                                }
                                if (!isBound) {//如果Binder跳过,使用原来的方法进行处理
                                        TextView _v = (TextView)v;
                                        _v.setText((String)data.get(from[i]));
                                }
                        }
                }
        }
}

不过对于我来说,尽量不重写SDK没有建议你复写的方法,因为复写之后性能和安全很难保证,最好能让原SDK去处理。继续查看源代码,发现当我们返回的类型不是一个view类型,而是一个确切的布局类型时,sdk就能识别,并不再当TEXTview处理,所以我的处理方案是将convertview转换成我那个layout文件的布局类型:

  @Override
	        public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
	        	  ItemViewHolder holder = null;
		           if(convertView == null){
		        	   holder=new ItemViewHolder();
		        	   convertView = inflater.inflate(R.layout.down_exam_vitem, null);
		        	   holder.itemImg = (ImageView)convertView.findViewById(R.id.exam_vitem_img);
		        	   convertView.setTag(holder);
		           }else{
						holder = (ItemViewHolder)convertView.getTag();
		           }
		           itemView = (RelativeLayout)convertView;
		           return itemView;
	        }
时间: 2024-08-01 19:55:42

android expandlistview group imageview报错的解决办法的相关文章

20170514002Oracle 11g R2安装过程中遇到的报错及解决办法

Oracle 11g R2安装过程中遇到的报错及解决办法 1.提示Check if the DISPLAYvariable is set.    Failed<<<< 解决方案: #xhost +  //切换到root用户输入 #su – Oracle  //切换到oracle用户 $./runInstaller  //执行安装程序 xhost 是用来控制X server访问权限的. 通常当你从hostA登陆到hostB上运行hostB上的应用程序时, 做为应用程序来说,hostA

调用系统命令 system-config-kickstart 报错,解决办法如下

[[email protected] ~]# system-config-kickstart Xlib:  extension "RANDR" missing on display "localhost:10.0". /usr/share/system-config-kickstart/kickstartGui.py:103: GtkWarning: GtkSpinButton: setting an adjustment with non-zero page si

单元测试时候使用[ClassInitialize]会该方法必须是静态的公共方法,不返回值并且应采用一个TestContext类型的参数报错的解决办法

using Microsoft.VisualStudio.TestTools.UnitTesting; 如果该DLL应用的是 C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\PublicAssemblies\Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll 版本为9.0.0.0 的话,则使用[ClassInitialize]会该方法必须是静态的公共方法,不返回值并且

Loadrunner参数化连接oracle、mysql数据源报错及解决办法

Loadrunner参数化连接oracle.mysql数据源报错及解决办法 (本人系统是Win7 64,  两位小伙伴因为是默认安装lr,安装在 最终参数化的时候,出现连接字符串无法自动加载出来: 最后通过安装在,问题到此解决 1.通过数据库连接参数化大量数据,电脑本地已经成功安装了数据库驱动,且本地可以配置数据源成功,在loadrunner 中配置数据源却找不到对应的数据库驱动. ----A:检查当前loadrunner工具的版本,是32位还是64位(目前还没有64位的),32位是不能安装64

Base64 报错 的解决办法 (Base-64 字符数组或字符串的长度无效。, 输入的不是有效的 Base-64 字符串,因为它包含非 Base-64 字符、两个以上的填充字符,或者填充字符间包含非法字符。)

Base64 报错 的解决办法, 报错如下:1. FormatException: The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or a non-white space character among the padding characters. ]    System.Convert.FromBase64Str

安装kvm虚拟机报错,解决办法

创建虚拟磁盘文件 [[email protected] ~]# qemu-img create -f qcow2 -o preallocation=metadata dev_beyond.img 8G Formatting 'dev_beyond.img', fmt=qcow2 size=8589934592 encryption=off cluster_size=65536 preallocation='metadata' 参数说明: -o preallocation=metadata 预分配

火狐浏览器扩展插件报错sec_error_ocsp_old_response解决办法

      firefox有个证书在线验证的功能,默认开启.每次访问https网站时,火狐会访问ocsp.digicert.com以查询证书合法性,如果这个网站不能访问则默认https证书是无效的,停止加载网页并报错. 解决办法:

SQLSERVER 创建ODBC 报错的解决办法 SQLState:&#39;01000&#39;的解决方案

错误详情如下: SQLState:'01000' SQL Server 错误:14 [Microsoft][ODBC SQL Server Driver][DBNETLIB] ConnectionOpen (Invalid Instance()). 连接失败: SQLState:'08001' SQL Server 错误:14 [Microsoft][ODBC SQL Server Driver][DBNETLIB] 无效的连接. 解决办法: 在创建ODBC数据源的步骤中,点击“客户端配置”,勾

关于 Notice: Undefined index: 2 in C:\wamp\www\...报错 的解决办法

首先,这个不是错误,是warning. 方法1:服务器配置修改 (Apache服务器中的php.ini配置文件)       修改php.ini配置文件,error_reporting = E_ALL & ~E_NOTICE 方法2:对变量进行初始化,规范书写(比较烦琐,因为有大量的变量). 方法3:每个文件头部加上:error_reporting(0); 如果不行,只有打开php.ini,找到display_errors,设置为display_errors = Off.以后任何错误都不会提示.