可折叠式的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 的判断,已经满足了我的需求,当然如果需要别的控件的话,可以再加其他判断。
这样比自定义适配器省事多了
在网上查了些资料大部分提供的解决方案是自定义一个Adapter 继承BaseExpandableListAdapte
SimpleExpandableListAdap
private void bindView(View view, Map<String, ?> data, String[] from, int[] to) {
果然,他将每一项都看成一个TextView 了,难怪不能添加图片,我们只需添加小小判断就可满足需求
修改后如下:
private void bindView(View view, Map<String, ?> data, String[] from, int[] to) {
我这里只加了ImageView 的判断,已经满足了我的需求,当然如果需要别的控件的话,可以再加其他判断。
这样比自定义适配器省事多了