Android technology 18: Android Adapter class details, androidadapter

Source: Internet
Author: User

Android technology 18: Android Adapter class details, androidadapter

1. Adapter design mode

In Android, the adapter interface has many implementations, such as ArrayAdapter, BaseAdapter, CursorAdapter, SimpleAdapter, and SimpleCursorAdapter. They correspond to different data sources respectively. For example, ArrayAdater corresponds to the List and array data sources, while CursorAdapter corresponds to the Cursor object (generally the record set obtained from the database ). All these adapters require the getView method to return the View object displayed in the current list. When the Model changes, the BaseAdapter. notifyDataSetChanged method is called to notify the component that the data has changed. At this time, the Adapter calls the getView method to re-display the content of the component. When the data displayed in the component changes, such as deleting an item table, the getView method in the component callback Adapter is used to re-display the content in the component. When the data displayed in the component changes, such as deleting an item table, the component notifies the Adapter to delete the corresponding records in the Model, and then calls BaseAdapter. the notifyDataSetChanged method changes the display data in the component. In a word, both data changes and component display data transmission changes require Adpter as a bridge to synchronize views and models.

Adapter is a very obvious MVC pattern in Android.

2. Internal implementation principle of the Adapter

The following uses ListView and BaseAdapter as examples to describe the principle.

Principle: each item in the List in ListView requests a View from the adapter, and then the getView method in the Adapter creates a new View or obtains the View to modify the ViewHoler content from the cache area, and then displays it. In Android, The View cache is provided for us. Not every new item needs to create a new View. In Android, a component called Recycler is implemented.

We can see that AndroidListView only creates a new View when convertView is empty at the beginning. This not only reduces the memory pressure, but also improves the performance and user experience of ListView sliding updates.

3. Code demonstration

ListView + BaseAdapter, MyAdaoter inherits BaseAdapter and implements its method. The key is getView ().

1 class MyAdapter extends BaseAdapter {2 3 private List <Map <String, String> datas = new ArrayList <Map <String, String> (); 4 private LayoutInflater mInflater; 5 private static final int TYPE_ITEM = 0; 6 private static final int TYPE_SEPARATOR = 1; 7 private static final int TYPE_MAX_COUNT = TYPE_SEPARATOR + 1; 8 private TreeSet mSeparatorsSet = new TreeSet (); 9 10 public MyAdapter () {11 mInflater = (La YoutInflater) getSystemService (Context. LAYOUT_INFLATER_SERVICE); 12} 13 // add project 14 public void add (Map <String, String> item) {15 datas. add (item); 16 notifyDataSetChanged (); 17} 18 // add the split column 19 public void addSeparator (Map <String, String> item) {20 datas. add (item); 21 mSeparatorsSet. add (datas. size ()-1); 22 notifyDataSetChanged (); 23} 24 @ Override25 public int getItemViewType (int position) {26 return mSeparatorsSe T. contains (position )? TYPE_SEPARATOR: TYPE_ITEM; 27} 28 29 @ Override30 public int getViewTypeCount () {31 return TYPE_MAX_COUNT; 32} 33 @ Override34 public int getCount () {35 return datas. size (); 36} 37 38 @ Override39 public Object getItem (int position) {40 41 return datas. get (position); 42} 43 44 @ Override45 public long getItemId (int position) {46 47 return position; 48} 49 50 @ Override51 public View getView (int position, View ConvertView, ViewGroup parent) {52 View view = convertView; 53 int type = getItemViewType (position); 54 ViewHolder viewHolder = null; 55 ViewHolder1 viewHolder1 = null; 56 if (view = null) {57 if (type = TYPE_ITEM) {58 59 view = mInflater. inflate (R. layout. item1, null); 60 viewHolder = new ViewHolder (); 61 viewHolder. textview1 = (TextView) view. findViewById (R. id. title); 62 viewHolder. textview2 = (TextView) view. findViewById (R. Id. num); 63 view. setTag (viewHolder); // setTag (Onbect) in the View indicates to add an extra data to the View. In the future, you can use getTag () to retrieve the data. 64} else {65 view = mInflater. inflate (R. layout. item2, null); 66 viewHolder1 = new ViewHolder1 (); 67 viewHolder1.textview1 = (TextView) view. findViewById (R. id. title); 68 view. setTag (viewHolder1); 69} 70} 71 72 if (type = TYPE_ITEM) {73 viewHolder = (ViewHolder) view. getTag (); 74 viewHolder. textview1.setText (datas. get (position ). get ("title"); 75 viewHolder. textview2.setText (datas. get (position ). get ("num"); 76} else {77 viewHolder1 = (ViewHolder1) view. getTag (); 78 viewHolder1.textview1. setText (datas. get (position ). get ("title"); 79 view. setBackgroundColor (Color. BLUE); 80} 81 return view; 82} 83 84}
1 public static class ViewHolder{2         public TextView textview1;3         public TextView textview2;4 }5     6 public static class ViewHolder1{7         public TextView textview1;8 }

ViewHolder and ViewHolder1 correspond to two different views, one is item and the other is split column.

Item1.xml

 1 <LinearLayout 2      xmlns:android="http://schemas.android.com/apk/res/android" 3      android:layout_width="fill_parent" 4      android:layout_height="fill_parent" 5      android:padding="10dip"> 6      7     <TextView  8         android:id="@+id/title" 9         android:layout_width="fill_parent"10         android:layout_height="wrap_content"11         android:gravity="left"12         android:textSize="18sp"13         android:layout_weight="1"/>14     <TextView 15         android:id="@+id/num"16         android:layout_width="fill_parent"17         android:layout_height="wrap_content"18         android:gravity="center_horizontal"19         android:textSize="18sp"20         android:layout_weight="1"/>21 </LinearLayout>

Item2.xml

 1 <LinearLayout 2      xmlns:android="http://schemas.android.com/apk/res/android" 3      android:layout_width="fill_parent" 4      android:layout_height="fill_parent"> 5      6     <TextView  7         android:id="@+id/title" 8         android:gravity="left" 9         android:textSize="22sp"10         android:background="#BCD2EE"11         android:layout_width="fill_parent"12         android:layout_height="wrap_content"13         android:padding="5dip"14     />15 16 </LinearLayout>

Initialize data

 1 private void init(MyAdapter adapter){ 2         Map<String,String> map=null; 3         for(int i=0;i<100;i++){ 4             if(i%5!=0){ 5             map=new HashMap<String, String>(); 6             map.put("title", "title"+i); 7             map.put("num", "num"+i); 8             adapter.add(map); 9             }else{10                 map=new HashMap<String, String>();11                 map.put("title", "S"+i/5);12                 adapter.addSeparator(map);13             }14         }15     }

Display Effect

 


Understanding the Adapter in android Development

Apapter makes it easier and more flexible to bind data to controls... It provides Sub-views for containers and uses the data and metadata of views to build each sub-view.
ArrayAdapter, simpleCursorAdapter, and cursorAdapter
ResourceCursorAdapter
If you need a custom adapter, you can extend the abstract class BaseAdapter.

GetVIew () of the Android Adapter ()

ViewGroup parent is the component where you set the adapter. It encapsulates a viewGroup to hold the item.
Position is the number of items you select starting from 0.
ConvertView is the layout or component of an item.

Override Adaper to extends BaseAdapter {
}
There are many examples of method rewriting on the Internet, but the parameter of the method to be rewritten cannot be set. You can add variables in the adapter class you override to implement data transmission.

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.