The difference between using view. settag () in getview () in a custom adapter and not using it.

Source: Internet
Author: User

Tag: Android tag

First, let's look at the use of tags.

@ Overridepublic view getview (INT position, view, viewgroup group Group) {viewholder holder = new viewholder (); If (view = NULL) {view = Inflater. inflate (R. layout. note_list_item, null); // loads the layout file of the list item. holder. title = (textview) view. findviewbyid (R. id. note_title); holder. createtime = (textview) view. findviewbyid (R. id. note_createtime); holder. clock = (imageview) view. findviewbyid (R. id. note_clock); view. settag (Holder ); // Bind the view and holder} else {holder = (viewholder) view. gettag () ;}string Title = items. get (position); Title = title. length ()> 10? Title. substring (0, 10) + "... ": title; holder. title. settext (title); holder. createtime. settext (times. get (position ));}
As you can see, this example uses a special viewholder class to save the component.

Private class viewholder {
Private textview title;
Private textview createtime;
Private imageview clock;
}

Let's take a look at the case where tags are not used:

@ Override

@Override                public View getView(int position, View convertView, ViewGroup parent) {                        if(convertView == null) {                                   convertView = mInflater.inflate(R.layout.multiple_checkbox_main_row, null);                        }                            TextView tN = (TextView) convertView.findViewById(R.id.multiple_title);     tN.setText((String)mList.get(position).get(NAME));    TextView tP = (TextView) convertView.findViewById(R.id.multiple_summary);                           tP.setText((String)mList.get(position).get(PHONE_NUMBER));}
You can find that the view. findviewbyid (XXX) method is called every time you do not use tags to obtain the view.

When tag is used, the view. findviewbyid (XXX) method does not need to be called because the control has been saved to the tag.

Tag is equivalent to a cache, which improves program performance.



The difference between using view. settag () in getview () in a custom adapter and not using it.

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.