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.