Android expandlistview group imageview error Solution
If imageview is found in the expandListView group, an error is returned. The system prompts that imageview cannot be converted to TextView. Check the source code and find that SimpleExpandableListAdapter has the following methods:
private void bindView(View view, Map
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])); } } }
We can see that bindView treats all views as textviews. There is no defense against the old brother simpleAdapter. You can automatically determine the type binding. The common method on the Internet is to rewrite the bindView method:
Public class MyExpandableListAdapter extends BaseExpandableListAdapter {private void bindView (View view, Map
Data, String [] from, int [] to) {int len =. 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) {// If the Binder is not empty, use the Binder to process isBound = mViewBinder. setViewValue (v, data. get (from [I]), text);} if (! IsBound) {// If the Binder is skipped, use the original method to process TextView _ v = (TextView) v; _ v. setText (String) data. get (from [I]) ;}}}
But for me, try not to rewrite the SDK and you are not recommended to rewrite the method, because the performance and security are difficult to guarantee after the rewrite, it is best to let the original SDK handle it. Continue to check the source code and find that the sdk can recognize the returned type when it is not a view type but an exact layout type, instead of TEXTview processing, so my solution is to convert convertview to the layout type of my layout file:
@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; }