BaseAdapter obtains the View level 3, which is baseadapterview.

Source: Internet
Author: User

BaseAdapter obtains the View level 3, which is baseadapterview.

Before obtaining a View, the BaseAdapter must be associated with the data source.

You can use the following constructor:

Private List <ItemBean> baseListItems;
Private LayoutInflater mInflate; // layout Loader
Public MyBaseAdapter (Context context, List <ItemBean> listItems ){
BaseListItems = listItems; // associate the data source with the data adapter
MInflate = LayoutInflater. from (context); // initialize the layout Loader
}

 

BaseAdapter obtains three types of views: simple, simple, and literary.

Funny comparison: You need to create a View multiple times. The buffer mechanism of the list component (ListView, GridView) is not fully utilized, which is suspected of wasting a lot of resources.

  @Override
  public View getView(int i, View convertView, ViewGroup viewGroup) {
View view = mInflate. inflate (R. layout. simple_item, null); // duplicate View creation. mInflate is the layout loader, which can be obtained from the interface object of the current Adapter to be used.
ImageView imageView = (ImageView) view. findViewById (R. id. header );
TextView personName = (TextView) view. findViewById (R. id. personName );
TextView desc = (TextView) view. findViewById (R. id. desc );
// Create the Bean object corresponding to the list item
BaseItemBean itemBean = baseListItems. get (I );
// Assign values to components
ImageView. setImageResource (itemBean. Header );
PersonName. setText (itemBean. PersonName );
Desc. setText (itemBean. Desc );
Return view;
  }   

Normal: The ListView cache mechanism is used.

@Override
public View getView(int i, View convertView, ViewGroup viewGroup) {
If (convertView = null) {// determines whether the view corresponding to the current list item layout exists in the cache pool. if it is null, use the layout loader for conversion. Otherwise, use
ConvertView = mInflate. inflate (R. layout. simple_item, null); // avoids repeated convertView creation.
}
ImageView imageView = (ImageView) convertView. findViewById (R. id. header );
TextView personName = (TextView) convertView. findViewById (R. id. personName );
TextView desc = (TextView) convertView. findViewById (R. id. desc );
// Create the Bean object corresponding to the list item
BaseItemBean itemBean = baseListItems. get (I );
// Assign values to components
ImageView. setImageResource (itemBean. Header );
PersonName. setText (itemBean. PersonName );
Desc. setText (itemBean. Desc );
Return convertView;
  }

Literary Style: although the listView cache mechanism is used to avoid repeated creation of convertView, multiple findViewByID () operations still exist, wasting a lot of resources, to avoid this situation, use the internal class ViewHolder...

  @Override
public View getView(int i, View convertView, ViewGroup viewGroup) {
      
ViewHolder viewHolder;
If (convertView = null ){
ConvertView = mInflate. inflate (R. layout. simple_item, null );
ViewHolder = new ViewHolder (); // initialize ViewHolder
// After convertView is created, save the components in the list items to ViewHolder.
ViewHolder. Header = (ImageView) convertView. findViewById (R. id. header );
ViewHolder. PersonName = (TextView) convertView. findViewById (R. id. personName );
ViewHolder. Desc = (TextView) convertView. findViewById (R. id. desc );
// Bind ViewHolder to convertView through setTag
ConvertView. setTag (viewHolder );
} Else {
// When convertVIew is not empty, get the ViewHolder object through getTag
ViewHolder = (ViewHolder) convertView. getTag ();

}
// Assign values to convertView Components
BaseItemBean itemBean = baseListItems. get (I );
ViewHolder. Header. setImageResource (R. mipmap. ic_launcher );
ViewHolder. PersonName. setText (itemBean. PersonName );
ViewHolder. Desc. setText (itemBean. Desc );
Return convertView;
}
// Create an internal class ViewHolder. each variable in the class must correspond to each unit component of the list item.
Public class ViewHolder {
Public ImageView Header;
Public TextView PersonName;
Public TextView Desc;
}
Conclusion: in the development process, we should try our best to use the literary style, which is conducive to making full use of resources and optimizing the development efficiency.




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.