When I encountered an Adapter while doing a small exercise, I found that I had not recorded it before.
Here we will introduce:
In fact, the Adapter serves as a bridge between data and views. The data is processed in the adapter and then displayed on the ListView.
There are many types of adapters, including ArrayAdapter <T>, BaseAdapter, CursorAdapter, HeaderViewListAdapter, ListAdapter, ResourceCursorAdapter, SimpleAdapter, SimpleCursorAdapter, SpinnerAdapter, and WrapperListAdapter.
ArrayAdapter <T> is used as an example.
An example I wrote myself:
There are two classes. One is the main interface Activity, which is used to process input and display. At the bottom, you can refer to the last one and see the layout as follows:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Name:" /> <EditText android:id="@+id/name" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout> <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Address:" /> <EditText android:id="@+id/addr" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout> <Button android:id="@+id/save" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Save" /> </LinearLayout> <?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Name:" /> <EditText android:id="@+id/name" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout> <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Address:" /> <EditText android:id="@+id/addr" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout> <Button android:id="@+id/save" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Save" />
</LinearLayout> the java code is as follows:
Public class LunchList extends Activity {List <Restaurant> model = new ArrayList <Restaurant> (); ArrayAdapter <Restaurant> adapter = null; @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); Button save = (Button) findViewById (R. id. save); save. setOnClickListener (onSave); ListView list = (ListView) findViewById (R. id. restaurants); adapter = new ArrayAdapter <Restaurant> (this, android. r. layout. simple_list_item_1, model); // This line of code explains list below. setAdapter (adapter); // set the configured adapter} private View for ListView. onClickListener onSave = new View. onClickListener () {public void onClick (View v) {Restaurant r = new Restaurant (); EditText name = (EditText) findViewById (R. id. name); EditText address = (EditText) findViewById (R. id. addr); r. setName (name. getText (). toString (); r. setAddress (address. getText (). toString (); RadioGroup types = (RadioGroup) findViewById (R. id. types); switch (types. getCheckedRadioButtonId () {case R. id. sit_down: r. setType ("sit_down"); break; case R. id. take_out: r. setType ("take_out"); break; case R. id. delivery: r. setType ("delivery"); break;} adapter. add (r); // each added entry will be added to the adapter }};} public class LunchList extends Activity {List <Restaurant> model = new ArrayList <Restaurant> (); arrayAdapter <Restaurant> adapter = null; @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); Button save = (Button) findViewById (R. id. save); save. setOnClickListener (onSave); ListView list = (ListView) findViewById (R. id. restaurants); adapter = new ArrayAdapter <Restaurant> (this, android. r. layout. simple_list_item_1, model); // This line of code explains list below. setAdapter (adapter); // set the configured adapter} private View for ListView. onClickListener onSave = new View. onClickListener () {public void onClick (View v) {Restaurant r = new Restaurant (); EditText name = (EditText) findViewById (R. id. name); EditText address = (EditText) findViewById (R. id. addr); r. setName (name. getText (). toString (); r. setAddress (address. getText (). toString (); RadioGroup types = (RadioGroup) findViewById (R. id. types); switch (types. getCheckedRadioButtonId () {case R. id. sit_down: r. setType ("sit_down"); break; case R. id. take_out: r. setType ("take_out"); break; case R. id. delivery: r. setType ("delivery"); break;} adapter. add (r); // each added entry is added to the adapter }};}
For the above explanation:
1. the adapter serves as a bridge between data and views
2. in this small example, an array is to be displayed. We use ArrayAdapter, array adapter, and the Data Type <> is of the Restaurant type (defined below ), the data type of the data can also be of another object type.
3. adapter = new ArrayAdapter <Restaurant> (this, android. R. layout. simple_list_item_1, model );
This Code creates an array adapter with three parameters. The first parameter is the context, which is the current Activity, and the second parameter is a built-in layout in the android sdk, there is only one TextView in it. This parameter indicates that the layout of each piece of data in our array is this view, that is, each piece of data is displayed on this view; the third parameter is the data we want to display. This data exists in the form of List <Restaurant>. Of course, there is no data in this array when we set it, you can call the adapter for data processing. add (r); added.
Based on these three parameters, listView traverses each piece of data in adapterData and reads one piece, which is displayed in the layout corresponding to the second parameter. This forms the listView we see.