To use a widget in Android, there are two things: style and data.
In the first article, our ListView uses the default style Android. R. layout. simple_list_item_1 provided by android. This style is actually a TextView for each item. We declare a List and assign values to the data. Finally, we use this code to bind these items to the ListView:
ArrayAdapter Adpater = new ArrayAdapter (This, android. R. layout. simple_list_item_1, data );
Overview
Therefore, the main learning content of this article is the Adpter used in ListView.
Speaking of Adapter, the first thing I think of is the Adapter mode. Although the design mode is not very good at learning, this word is still remembered. Let's take a look at the description of the Adapter in the API:
An Adapter object acts as a bridge betweenAdapterView
And the underlying data for that view. The Adapter provides access to the data items. The Adapter is also responsible for makingView
For each item in the data set. (the Adapter object acts as a bridge between AdapterView and data. the Adapter provides a path to data items. The Adapter is also responsible for generating a view for each data item in the dataset)
Subclass of Adapter:
The above two figures are from (http://www.cnblogs.com/devinzhang/archive/2012/01/20/2328334.html)
The Adapter has many subclasses. ListActivity uses ListAdpter. ListActivity provides the setListAdapter method to access and use Adpter.
public void setListAdapter(ListAdapter adapter) { synchronized (this) { ensureList(); mAdapter = adapter; mList.setAdapter(adapter); } }
The description of ListAdpter in API is as follows:
ExtendedAdapter
That is the bridge betweenListView
And the data that backs the list. frequently that data comes from a Cursor, but that is not required. the ListView can display any data provided that it is wrapped in a ListAdapter. (ListAdapter inherits the Adapter and serves as a bridge between the ListView and the dataset. Generally, the data comes from Cursor, but it is not necessary. ListView can display any data encapsulated by ListAdapter)
However, ListAdpter is an interface. Instead of using it directly, we use the ArrayAdapter that implements this interface. , SimpleAdapter, SimpleCursorAdapter.
Next, I will introduce the usage of these adapter types:
ArrayAdapter
ArrayAdapter is a wildcard adapter provided by Android, which is convenient and has several styles defined by default.
ArrayAdapter provides several constructor methods:
Public Constructors |
|
ArrayAdapter (Context context, int resource) Constructor |
|
ArrayAdapter (Context context, int resource, int textViewResourceId) Constructor |
|
ArrayAdapter (Context context, int resource, T [] objects) Constructor |
|
ArrayAdapter (Context context, int resource, int textViewResourceId, T [] objects) Constructor |
|
ArrayAdapter (Context context, int resource, List Objects) Constructor |
|
ArrayAdapter (Context context, int resource, int textViewResourceId, List Objects) Constructor |
Parameters
Context |
The current context. |
Resource |
The resource ID for a layout file containing a layout to use when instantiating views. (id of The layout file corresponding to The Item) |
TextViewResourceId |
The id of the TextView within the layout resource to be populated (id of The layout file corresponding to TextView) |
Objects |
The objects to represent in the ListView. (The dataset to be displayed) |
Android provides several layout files for Item query: for example, android. R. layout. simple_list_item_1, which is used in the first example, has only one TextView in the layout file.
Let's take a look at its source code:
We can also use the configuration files provided by other Android
Note thatBecause the android. R. layout. simple_list_item_1 style I used in the first example has only one TextView, so I can use constructors with only three parameters.
ArrayAdapter Adpter = new ArrayAdapter (This, android. R. layout. simple_list_item_1, data );
If there is more than one view in the configuration file, you need to specify the view to which the data is stored. For example, if simple_list_item_single_choice is used to have two textviews in the configuration file, the constructor with four parameters is required.
ArrayAdapter Adpter = new ArrayAdapter (This, android. R. layout. simple_list_item_single_choice, android. R. id. text1, data );
The two running results are as follows:
SimpleAdapter
Compared with ArrayAdapter, SimpleAdapter provides a more flexible way to build ListView.
Let's take a look at its constructor:
Public Constructors |
|
SimpleAdapter (Context context, List > Data, int resource, String [] from, int []) Constructor |
Parameters
Context |
The context where the View associated with this SimpleAdapter is running |
Data |
A List of Maps. Each entry in the List corresponds to one row in the list. The Maps contain the data for each row, And shoshould include all the entries specified in "from" |
Resource |
Resource identifier of a view layout that defines the views for this list item. The layout file shoshould include at least those named views defined in "" |
From |
A list of column names that will be added to the Map associated with each item. |
To |
The views that shocould display column in the "from" parameter. These shocould all be TextViews. The first N views in this list are given the values of the first N columns in the from parameter. |
The second parameter indicates the dataset to be displayed. Each Item in List corresponds to the Item in ListView. The data in Map corresponds to the columns in Item. Note that the key in Map should be consistent with the from] parameter. The third parameter is the configuration file that defines the Item style. The fourth parameter is an array that lists the keys of the data we want to display in map. The fifth parameter is also an array, listing the view id in the Item for the actual data. The order in to corresponds to the order in from.
Here is a small example: item. xml
Activity:
Package com. example. listview4; import java. util. arrayList; import java. util. hashMap; import java. util. list; import java. util. map; import android. OS. bundle; import android. widget. simpleAdapter; import android. app. listActivity; public class MainActivity extends ListActivity {@ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); SimpleAdapter adapter = new SimpleAdapter (this, getData (), R. layout. item, new String [] {"image", "text"}, new int [] {R. id. image, R. id. text}); setListAdapter (adapter);} private List
> GetData () {List
> List = new ArrayList
> (); Map
Map = new HashMap
(); Map. put ("image", R. drawable. ic_launcher); map. put ("text", "Test 1"); list. add (map); Map
Map1 = new HashMap
(); Map1.put ("image", R. drawable. ic_launcher); map1.put ("text", "Test 2"); list. add (map1); Map
Map2 = new HashMap
(); Map2.put ("image", R. drawable. ic_launcher); map2.put ("text", "Test 3"); list. add (map2); return list ;}}
The running result is as follows:
SimpleCursorAdapterSimpleCursorAdapter provides a method to bind data in the database to ListView. Let's take a look at the constructor method:
Public Constructors |
|
SimpleCursorAdapter (Context context, int layout, Cursor c, String [] from, int []) This constructor was deprecated in API level 11. this option is discouraged, as it results in Cursor queries being stored med on the application's UI thread and thus can cause poor responsiveness or even Application Not Responding errors. as an alternative, useLoaderManager WithCursorLoader . |
|
SimpleCursorAdapter (Context context, int layout, Cursor c, String [] from, int [] to, int flags) Standard constructor. |
Parameters
Context |
The context where the ListView associated with this SimpleListItemFactory is running |
Layout |
Resource identifier of a layout file that defines the views for this list item. The layout file shocould include at least those named views defined in "" |
C |
The database cursor. Can be null if the cursor is not available yet. |
From |
A list of column names representing the data to bind to the UI. Can be null if the cursor is not available yet. |
To |
The views that shoshould display column in the "from" parameter. These shoshould all be TextViews. The first N views in this list are given the values Of the first N columns in the from parameter. Can be null if the cursor is not available yet. |
Flags |
Flags used to determine the behavior of the adapter, as perCursorAdapter(Context, Cursor, int) . |
The parameter is similar to the previous one.
The following code is written in reference to the example provided in the API. However, the method for getting contact information in the Code is not recommended in the current API, so it is only for learning reference.
package com.example.listview5;import android.app.ListActivity;import android.database.Cursor;import android.os.Bundle;import android.provider.Contacts.People;import android.widget.ListAdapter;import android.widget.SimpleCursorAdapter;@SuppressWarnings("deprecation")public class MainActivity extends ListActivity { @Override protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); // Query for all people contacts using the Contacts.People convenience class. // Put a managed wrapper around the retrieved cursor so we don't have to worry about // requerying or closing it as the activity changes state. Cursor mCursor = this.getContentResolver().query(People.CONTENT_URI, null, null, null, People.TIMES_CONTACTED); startManagingCursor(mCursor); // Now create a new list adapter bound to the cursor. // SimpleListAdapter is designed for binding to a Cursor. ListAdapter adapter = new SimpleCursorAdapter( this, // Context. android.R.layout.two_line_list_item, mCursor, new String[] {People.NAME, People.TIMES_CONTACTED}, new int[] {android.R.id.text1, android.R.id.text2}); // Bind to our new adapter. setListAdapter(adapter); }}
In fact, we can see that the usage of SimpleCursorAdapter is similar to that of SimpleAdapter, except that the data source of the two is a number from the database, and the other is a List.