BaseAdapter usage (note)
Application in the adapter mode:
1. Reduced program Coupling
2. Easy to expand
BaseAdapter
Display and cache mechanism of ListView: Display is required, and the display is cached.
Basic Structure of BaseAdapter
-- Public int getCount (); number of data sets in the adapter
-- Public Object getItem (int position): obtains the data items corresponding to the specified index in the dataset.
-- Public long getItem (int position): obtains the ID of the specified row.
-- Public View getView (int position, ViewconverView, ViewGroup parent): obtains the display content of each Item.
Step 1: Create the layout file activity_main.xml and create a simple listView.
Create the item layout file item. xml, with one ImageView and two textviews
Activity_main.xml
Item. xml
Effect
Step 2: Create a Bean object ItemBean, encapsulate the content displayed in item, and create a data source in MainActivity. java on the home page.
ItemBean. java
public class ItemBean {public int ItemImageResid;public String Itemtitle;public String ItemContent;public ItemBean(int itemImageResid, String itemtitle, String itemContent) {super();ItemImageResid = itemImageResid;Itemtitle = itemtitle;ItemContent = itemContent;}}
MainActivity. java
Package com. example. sr; import java. util. arrayList; import java. util. list; import android. OS. bundle; import android. app. activity; import android. view. menu; public class MainActivity extends Activity {@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); List
ItemBeanList = new ArrayList
(); For (int I = 0; I <20; I ++) {itemBeanList. add (new ItemBean (R. drawable. ic_launcher, "I am the title" + I, "I am the content" + I ));}}}
Step 3: Create a New MyAdapter class, inherit from BaseAdapter, override the method in it, and implement the adapter
MyAdapter. java
Package com. example. sr; import java. util. list; import android. view. view; import android. view. viewGroup; import android. widget. baseAdapter; public class MyAdapter extends BaseAdapter {private List
MList; public MyAdapter (List
List) {// the data source is associated with the adapter mList = list;} @ Overridepublic int getCount () {// return the data size to be displayed in ListView // TODO Auto-generated method stubreturn mList. size () ;}@ Overridepublic Object getItem (int position) {// obtain the data item corresponding to the specified index in the dataset // TODO Auto-generated method stubreturn mList. get (position) ;}@ Overridepublic long getItemId (int position) {// get the ID of the specified row // TODO Auto-generated method stubreturn position ;} @ Overridepublic View getView (int arg0, View arg1, ViewGroup arg2) {// return the display content of each item // TODO Auto-generated method stubreturn null ;}}
The above is mainly the getView method, which has not yet been implemented. The following describes the three realms of the getView method implementation.
1. Funny Ratio
MyAdapter. java
Package com. example. sr; import java. util. list; import android. content. context; import android. view. layoutInflater; import android. view. view; import android. view. viewGroup; import android. widget. baseAdapter; import android. widget. imageView; import android. widget. textView; public class MyAdapter extends BaseAdapter {private List
MList; private LayoutInflater minflater; // layout loader object, which is used to convert an xml layout file to viewpublic MyAdapter (Context context, List
List) {// the data source is associated with the adapter. mList = list; minflater = LayoutInflater. from (context); // context the interface object of the current Adapter to be used} @ Overridepublic int getCount () {// return the data size to be displayed in ListView // TODO Auto-generated method stubreturn mList. size () ;}@ Overridepublic Object getItem (int position) {// obtain the data item corresponding to the specified index in the dataset // TODO Auto-generated method stubreturn mList. get (position) ;}@ Overridepublic long getItemId (int position) {// get the ID of the specified row // TODO Auto-generated method stubreturn position ;} @ Overridepublic View getView (int postion, View convertView, ViewGroup parent) {// return the display content of each item // TODO Auto-generated method stubView view = minflater. inflate (R. layout. item, null); // The first parameter needs to be loaded to item. xml layout file. The second parameter is generally set to nullImageView imageView = (ImageView) view. findViewById (R. id. iv_image); TextView title = (TextView) view. findViewById (R. id. TV _title); TextView content = (TextView) view. findViewById (R. id. TV _content); // obtain the data and assign it to the three controls ItemBean = mList. get (postion); imageView. setImageResource (bean. itemImageResid); title. setText (bean. itemtitle); content. setText (bean. itemContent); return view ;}}
Return to MainActivity. java to establish the contact between listView and MyAdapter.
MainActivity. java
Package com. example. sr; import java. util. arrayList; import java. util. list; import android. OS. bundle; import android. app. activity; import android. view. menu; import android. widget. listView; public class MainActivity extends Activity {@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); List
ItemBeanList = new ArrayList
(); For (int I = 0; I <20; I ++) {itemBeanList. add (new ItemBean (R. drawable. ic_launcher, "I am the title" + I, "I am the content" + I);} // create a contact between listView and MyAdapter ListView listView = (ListView) findViewById (R. id. lv_main); listView. setAdapter (new MyAdapter (this, itemBeanList); // The first parameter is context and the second parameter is the data source }}
Now, run the program and find that listView can display what we want.
Why is it just a simple and funny mix? ListView has a buffer mechanism. However, while getView ignores the buffer mechanism, it creates a new view to set the space, and its efficiency is low. The listView cache mechanism is not used at all.
2. Common
Modify the getView Method
@ Overridepublic View getView (int postion, View convertView, ViewGroup parent) {// return the display content of each item // TODO Auto-generated method stub // start with the funny ratio ---------- // View view = minflater. inflate (R. layout. item, null); // The first parameter needs to be loaded to item. xml layout file. The second parameter is usually null/* ImageView imageView = (ImageView) view. findViewById (R. id. iv_image); TextView title = (TextView) view. findViewById (R. id. TV _title); TextView content = (TextView) view. findViewById (R. id. TV _content); // obtain the data and assign it to the three controls ItemBean = mList. get (postion); imageView. setImageResource (bean. itemImageResid); title. setText (bean. itemtitle); content. setText (bean. itemContent); * // The End Of The funny ratio ----------- // common type. The parameter already has a converView, so check whether the view has been cached. If the image has been cached, you can directly use if (convertView = null) {convertView = minflater. inflate (R. layout. item, null);} ImageView imageView = (ImageView) convertView. findViewById (R. id. iv_image); TextView title = (TextView) convertView. findViewById (R. id. TV _title); TextView content = (TextView) convertView. findViewById (R. id. TV _content); ItemBean bean = mList. get (postion); imageView. setImageResource (bean. itemImageResid); title. setText (bean. itemtitle); content. setText (bean. itemContent); return convertView ;}
Using the cache feature of ListView, if a new View is created without caching, it is easy to calculate, but findViewById still wastes a lot of time
3. literary style (BEST)
An internal ViewHolder class is created, which is associated with the view and caches the components of the item. In this way, when setting content for the item, no findviewById is required, you just need to retrieve it from ViewHolder.
@ Overridepublic View getView (int postion, View convertView, ViewGroup parent) {// return the display content of each item // TODO Auto-generated method stub // start with the funny ratio ---------- // View view = minflater. inflate (R. layout. item, null); // The first parameter needs to be loaded to item. xml layout file. The second parameter is usually null/* ImageView imageView = (ImageView) view. findViewById (R. id. iv_image); TextView title = (TextView) view. findViewById (R. id. TV _title); TextView content = (TextView) view. findViewById (R. id. TV _content); // obtain the data and assign it to the three controls ItemBean = mList. get (postion); imageView. setImageResource (bean. itemImageResid); title. setText (bean. itemtitle); content. setText (bean. itemContent); * // The End Of The funny ratio ----------- // common type. The parameter already has a converView, so check whether the view has been cached. If the image has been cached, you can directly use ---------/* if (convertView = null) {convertView = minflater. inflate (R. layout. item, null);} ImageView imageView = (ImageView) convertView. findViewById (R. id. iv_image); TextView title = (TextView) convertView. findViewById (R. id. TV _title); TextView content = (TextView) convertView. findViewById (R. id. TV _content); ItemBean bean = mList. get (postion); imageView. setImageResource (bean. itemImageResid); title. setText (bean. itemtitle); content. setText (bean. itemContent); return convertView; * // normal end ----------------- ViewHolder viewHolder; if (convertView = null) {viewHolder = new ViewHolder (); convertView = minflater. inflate (R. layout. item, null); viewHolder. imageView = (ImageView) convertView. findViewById (R. id. iv_image); // Save the control to ViewHolder. title = (TextView) convertView. findViewById (R. id. TV _title); viewHolder. content = (TextView) convertView. findViewById (R. id. TV _content); convertView. setTag (viewHolder); // associate and save its control in viewHolder, avoiding findviewbyid instantiation} else {viewHolder = (ViewHolder) convertView. getTag ();} ItemBean bean = mList. get (postion); viewHolder. imageView. setImageResource (bean. itemImageResid); viewHolder. title. setText (bean. itemtitle); viewHolder. content. setText (bean. itemContent); return convertView;} class ViewHolder {// first create an internal class public ImageView imageView; public TextView title; public TextView content ;}
Not only the ListView cache is used, but also the ViewHolder class is used to cache the view for displaying data. This is the best way to avoid multiple queries for controls through findViewById.
Summary:
How ViewHolder optimizes BaseAdapter
-- Create a Bean object to encapsulate data
-- Create MyAdapter (inheriting BaseAdapter) to initialize the data List for ing in the constructor
-- Create a ViewHolder class and create a layout ing relationship
-- Determines ConverView. If it is null, The ViewHolder is created and the Tag is set. Otherwise, the ViewHolder is retrieved through the tag.
-- Set Data for controls in ViewHolder