Android Adapter, androidadapter
Adapter)
From the perspective of the Adapter, I think this is a very important knowledge point. The Adapter is used to help fill out the intermediate Bridge of data. To put it simply: display various data in the View as appropriate. Adapter has many interfaces, abstract classes, and subclasses that can be used. Here are some of the frequently used interfaces.
BaseAdapter, ArrayAdapter, and SimpleAdapter. In order to cooperate with these adapters, The ListView is used to demonstrate the use of the adapter in advance, which will be further summarized later.
I. ArrayAdapter (array adapter), which has certain limitations and can only display one line of text data
(1) Basic instances
Layout file:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <ListView android:id="@+id/ll1" android:layout_width="match_parent" android:layout_height="wrap_content"> </ListView></LinearLayout>
Java files
Package com. example. test3; import android. app. activity; import android. OS. bundle; import android. widget. arrayAdapter; import android. widget. listView; public class MainActivity extends Activity {// defines the private String [] datas = {"Zhang San", "Li Si", "Wang Wu", "Ma Zi ", "Xiaoqiang"}; private ArrayAdapter <String> adapter; private ListView listView; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); listView = (ListView) findViewById (R. id. ll1); // initialize the adapter = new ArrayAdapter <> (this, android. r. layout. simple_expandable_list_item_1, datas); listView. setAdapter (adapter );}}
:
(2) The preceding implementation can also create an xml file for Array resources under res \ value: arrays. xml, and then use entries in listview.
3. ArrayAdapter also supports generics, so the set must be written as follows:
4. ArrayAdapter parameters:
First parameter: context object
The second parameter: the style of each item, which can be provided by the system or customized as a TextView
Third parameter: data source, the data to be displayed
The style of the item provided by the system. Try it.
Simple_list_item1: a separate text box
Simple_list_item2: composed of two text boxes
Simple_list_item_checked
Simple_list_item_multiple_choice: both contain a check box.
Simple_list_item_single_choice: each has a single response
Ii. SimpleAdapter
(1) Basic instances
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <ListView android:id="@+id/ll1" android:layout_width="match_parent" android:layout_height="wrap_content"> </ListView></LinearLayout>
Define the style of the item to be implemented
<? Xml version = "1.0" encoding = "UTF-8"?> <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: orientation = "horizontal"> <ImageView android: id = "@ + id/image1" android: layout_width = "100dp" android: layout_height = "100dp" android: src = "@ mipmap/ic_launcher" android: layout_margin = "5dp"/> <LinearLayout android: id = "@ + id/ll2" android: layout_width = "match_parent" android: layout_height = "100dp" android: orientation = "vertical" android: layout_marginTop = "5dp" android: layout_marginLeft = "10dp"> <TextView android: id = "@ + id/text1" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "Haha" android: textSize = "30sp" android: layout_marginTop = "10dp"/> <TextView android: id = "@ + id/text2" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "Haha" android: textSize = "24dp" android: layout_marginTop = "10dp"/> </LinearLayout>
Java files
Package com. example. test3; import android. app. activity; import android. OS. bundle; import android. widget. listView; import android. widget. simpleAdapter; import java. util. arrayList; import java. util. hashMap; import java. util. list; import java. util. map; public class MainActivity extends Activity {// these three commonly appear at the same time private List <Map <String, Object> lists; private SimpleAdapter adapter; private ListView listView; // define the data private String [] theme = {"Zhang San", "Li Si", "Wang Wu"}; private String [] content = {"I am Zhang San, hello ", "I'm Li Si, hi", "I'm Wang Wu, hi"}; private int [] imageViews = {R. mipmap. ic_launcher, R. mipmap. ic_account, R. mipmap. ic_password}; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); listView = (ListView) findViewById (R. id. ll1); // prepare the data source lists = new ArrayList <> (); for (int I = 0; I <theme. length; I ++) {Map <String, Object> map = new HashMap <> (); map. put ("image", imageViews [I]); map. put ("theme", theme [I]); map. put ("content", content [I]); lists. add (map);} adapter = new SimpleAdapter (MainActivity. this, lists, R. layout. list_item, new String [] {"image", "theme", "content"}, new int [] {R. id. image1, R. id. text1, R. id. text2}); listView. setAdapter (adapter );}}
:
(2)
First parameter: context object
The second parameter: the data source is a set containing Map
Third parameter: Layout file of each item
The fourth parameter: new String [] {} array. Each item in the array must be the same as the key value stored in the map set in the second parameter.
Fifth parameter: new int [] {} array, Control id in item in the third parameter in the array.
3. BaseAdapter is used in many ways. Currently, BaseAdapter is used to achieve the same effect.
(1) The layout file and the display of each item are the same as those in SimpleAdapter above. They are mainly files in Java. We use BaseAdapter to inherit it and implement the abstract method in it.
Custom adapter
Package com. example. test3; import android. content. context; import android. text. layout; import android. view. layoutInflater; import android. view. view; import android. view. viewGroup; import android. widget. baseAdapter; import android. widget. imageView; import android. widget. textView; import java. util. arrayList; import java. util. list;/*** Created by coder-tu on 2016/1/13. */public class MyAdapter extends BaseAdapter {private List <Message> Datas; private Context mContext; public MyAdapter (List <Message> datas, Context mContext) {Datas = datas; this. mContext = mContext;}/*** number of returned items * @ return */@ Override public int getCount () {return Datas. size ();}/*** returns each item Object * @ param I * @ return */@ Override public Object getItem (int I) {return Datas. get (I);}/*** returns the id of each item * @ param I * @ return */@ Override public long getItemId (int I) {return I ;} /*** optimization is not performed temporarily, we will sort out BaseAdapter optimizations * @ param I * @ param view * @ param viewGroup * @ return */@ Override public View getView (int I, View view, ViewGroup viewGroup) {view = LayoutInflater. from (mContext ). inflate (R. layout. list_item, viewGroup, false); ImageView imageView = (ImageView) view. findViewById (R. id. image1); TextView textView1 = (TextView) view. findViewById (R. id. text1); TextView textView2 = (TextView) view. findViewById (R. id. text2); imageView. setImageResource (Datas. get (I ). getImageId (); textView1.setText (Datas. get (I ). getTheme (); textView2.setText (Datas. get (I ). getContent (); // The returned view cannot be a return view in the view ;}}
Then use
Package com. example. test3; import android. app. activity; import android. OS. bundle; import android. widget. listView; import java. util. arrayList; import java. util. list; public class MainActivity extends Activity {// these three commonly appear at the same time private List <Message> lists; private MyAdapter adapter; private ListView listView; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); listView = (ListView) findViewById (R. id. ll1); lists = new ArrayList <> (); lists. add (new Message (R. mipmap. ic_launcher, "Zhang San", "Hello, I am Zhang San"); lists. add (new Message (R. mipmap. ic_account, "Li Si", "Hello, I am Li Si"); lists. add (new Message (R. mipmap. ic_password, "Wang Wu", "Hello, I am Wang Wu"); adapter = new MyAdapter (lists, MainActivity. this); listView. setAdapter (adapter );}}
:
(2) The methods in BaseAdapter are described in detail in Java code.
(3) BaseAdapter optimization: three modes (simple, simple, and literary)
Teasing ratio: There is no optimization above. For short, it is a funny ratio. He does not use the listView cache mechanism, and views are repeatedly created to create findviewbyid. The code is not optimized above.
Normal: The listview cache mechanism is fully utilized, but the findviewbyid is used repeatedly.
Literary Style: This method is very good, that is, using the listView cache mechanism, and avoiding repeated findViewById
1. Create an internal class. 2. Determine whether convertView is empty. 3. Bind The viewHolder to the convertView through the setTag method.
Summary: for these three adapters, BaseAdapter is the most widely used, and is more free to use because of its small limitations.