The holes that use Holder in the Adapter, and the Adapter uses Holder

Source: Internet
Author: User

The holes that use Holder in the Adapter, and the Adapter uses Holder

When using the GridView and ListView, Zookeeper usually uses Holder to cache each item in the Adapter to improve efficiency. However, if the Holder is not used properly, this caching mechanism will lead to many unexpected problems, I would like to sum up my experience to avoid further attacks in the future.

Disordered content

  Updates the content of each item through position in the getView method of the Adapter. For the situation where attributes are set for each item based on judgment conditions, each attribute of each item must be assigned a value under each judgment condition. Otherwise, the content may be disordered when sliding ListView or GridView. For example, the following code calls a method in getView, if you use Holder, you can set attributes for the item background only under the if (position = mSelectPosition) Branch, during the Sliding Process, the background that should not have been a blue background turns blue:

Private void setFileInfo (FileInfo fileInfo, int position) {mFileHolder. mFilePathTxtView. setText (fileInfo. getFilePath (); mFileHolder. mVoiceTypeTxtView. setText (fileInfo. getVoiceType (); mFileHolder. mFileItemLayout. setTag (position); if (position = mSelectPosition) {mFileHolder. mFileItemLayout. setBackgroundColor (Color. BLUE);} else {// The else score of each item also needs to set the background for the item, otherwise the content will be disordered during the sliding process. mFileItemLayout. setBackgroundColor (Color. RED );}}

Although zookeeper is easy to handle, it is often encountered during development. For example, when you set different backgrounds, headshots, or texts for each item based on the judgment conditions, unexpected bugs may occur if you do not pay attention to them or you are lazy. You should pay more attention to each item when you need to modify multiple attributes at the same time, make sure that attributes are set for each view of the item under each condition score.

SetTag

Zookeeper adds a listener click event in the above Code. Because cache is used, getId cannot be used to obtain the specific item to be clicked, generally, we set a tag for each layout and get the specific tag in the listener callback, for example, the following:

Private void setFileInfo (FileInfo fileInfo, int position) {mFileHolder. mFilePathTxtView. setText (fileInfo. getFilePath (); mFileHolder. mVoiceTypeTxtView. setText (fileInfo. getVoiceType (); // For each setTag, use v. the getTag () method is used to obtain the mFileHolder. mFileItemLayout. setTag (position); mFileHolder. mFileItemLayout. setOnClickListener (new OnClickListener () {@ Override public void onClick (View v) {in T position = (Integer) v. getTag (); mSelectPosition = position; if (null! = MOnFileListOnClickListener) {mOnFileListOnClickListener. onItemClick (mFileInfoList. get (position);} policydatasetchanged () ;}}); if (position = mSelectPosition) {mFileHolder. mFileItemLayout. setBackgroundColor (Color. BLUE);} else {mFileHolder. mFileItemLayout. setBackgroundColor (Color. RED );}}

If zookeeper is encoded in the preceding way, you can click a program after the program is running, and the following exception is reported:

02-01 15:57:49.340: E/AndroidRuntime(1949): java.lang.ClassCastException: java.lang.Integer cannot be cast to com.uperone.view.FileListAdapter$FileHolder02-01 15:57:49.340: E/AndroidRuntime(1949): at com.uperone.view.FileListAdapter.getView(FileListAdapter.java:72)

The specific cause of zookeeper is also caused by the Holder cache, because in the getView method, I set a tag for convertView to achieve Holder reuse:

@Overridepublic View getView(int position, View convertView, ViewGroup parent) {    if( null == convertView ){        convertView = mLayoutInflater.inflate( R.layout.list_file_item_layout, null);        mFileHolder = new FileHolder();        mFileHolder.mFileItemLayout = ( LinearLayout )convertView.findViewById(R.id.fileItemLayoutId);        mFileHolder.mFilePathTxtView = ( TextView )convertView.findViewById(R.id.filePathTxtId);        mFileHolder.mVoiceTypeTxtView = ( TextView )convertView.findViewById(R.id.voiceTypeTxtId);        mFileHolder.mFileItemLayout.setClickable(true);        convertView.setTag( mFileHolder );    }else{        mFileHolder = ( FileHolder )convertView.getTag( );    }    if( !isDataEmpty() ){        setFileInfo( mFileInfoList.get( position ), position );    }    return convertView;}

When you click an item in ListView, The setFileInfo method for setting each item is in convertView. getTag () is called, so when the system calls the getView () method to update the ListView, the tag obtained by convertView is mHolder. if Holder is used for the tag set by mFileItemLayout, you cannot set tags and retrieve tags in multiple views in each item.

The common solution to the preceding exceptions is to listen to each item by using the setOnItemClickListener method at the place where the ListView is instantiated to avoid tag conflicts.

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.