Android expandlistview group imageview error Solution

Source: Internet
Author: User

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;        }

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.