The adapter is a bridge class that binds data to the UI. The adapter is responsible for creating a sub-view that displays each item and providing access to the lower-level data. It fills the data in listview, gridview, gallery. The Android system provides several adapters: arrayadapter <t>, baseadapter, cursoradapter, headerviewlistadapter, listadapter, resourcecursoradapter, simpleadapter, simplecursoradapter, spinneradapter, and wrapperlistadapter.
Baseadapter, arrayadapter, simplecursoradapter, etc. Shows the relationship among data, adapter, and view:
In most cases, you do not need to create your own Adapter. Android provides a series of adapters to bind data to the UI widget, as shown in. Because Android provides data and selection for displaying the view of each project, the adapter can quickly modify the appearance and functions of the control to be bound.
❑ Baseadapter is an abstract class that inherits from it and needs to implement many methods, so it has high flexibility;
Javasarrayadapter supports generic operations and usually requires the getview method. In special cases (combined with the data row ID), it is best to override getitemid () to make it easier to handle UI events (); arrayadapter is a common class that binds a view to a group of objects. By default, arrayadapter binds the tostring value of each object to the pre-defined textview control in layout.
❑ Simplecursoradapter can be used for simple text-only listview. It requires the cursor field to correspond to the ui id. to implement a more complex UI, You can override other methods. Simplecursoradapter binds the view to the cursor returned by the content provider query. Specify an XML layout definition, and then bind the values of each column in the dataset to a view in layout.
1. Example of baseadapter
1: /**
2: * song list Adapter
3: *
4: * @version 1.0
5: * @author linghu
6: */
7: public class AudioListAdapter extends BaseAdapter {
8:
9: private Context mContext;
10:
11: // song set
12: private ArrayList<Audio> mAudios;
13:
14: public AudioListAdapter(Context mContext, ArrayList<Audio> mAudios) {
15: this.mContext = mContext;
16: this.mAudios = mAudios;
17: }
18:
19: @Override
20: public int getCount() {
21: return mAudios != null ? mAudios.size() : 0;
22: }
23:
24: @Override
25: public Object getItem(int position) {
26: if ((mAudios != null && mAudios.size() > 0) && (position >= 0 && position < mAudios.size())) {
27: return mAudios.get(position);
28: }
29: return null;
30: }
31:
32: /**
33: * if the object data in the set comes from the database, we recommend that you use this method to return the ID of the object in the database.
34: */
35: @Override
36: public long getItemId(int position) {
37: if ((mAudios != null && mAudios.size() > 0) && (position >= 0 && position < mAudios.size())) {
38: return mAudios.get(position).getId();
39: }
40: return position;
41: }
42:
43: @Override
44: public View getView(int position, View convertView, ViewGroup parent) {
45: // todo returned custom View
46: }