Hello, everyone! Today, to explain the use of Baseadapter (base adapter), the role of the adapter is primarily used to fill data such as (Spinner,listview,gridview). and (Spinner,listview,gridview) have their own adapter (remember trouble). But Baseadapter (a trick) is universal to them, why do you say so, first we look at the API documentation:
Let's take a look at the Baseadapter has implemented the ListAdapter and Spinneradapter interface, and the GridView adapter is the implementation of the ListAdapter interface, but only two-dimensional. So baseadapter is common to all three of them.
Let me take a look at the main usage of baseadapter. We define a class (for example: Myadapter) And this class inherits Baseadapter. Because it is implements ListAdapter and Spinneradapter interface, so to implement the inside of the method, the code is as follows (without any changes):
Private classMyadapterextendsbaseadapter{@Override Public intGetCount () {//TODO auto-generated Method Stub return0; } @Override PublicObject GetItem (intarg0) { //TODO auto-generated Method Stub return NULL; } @Override Public LongGetitemid (intposition) { //TODO auto-generated Method Stub return0; } @Override PublicView GetView (intposition, View Convertview, ViewGroup parent) { //TODO auto-generated Method Stub return NULL; } }
In order to facilitate people to understand, the usual writing a simple demo, everyone according to my steps to OK.
The first step: Create a new Android project named Baseadapterdemo.
Step Two: Modify the Main.xml code 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" > <TextView android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:text= "Welcome to Mr Wei ' s Blog"/> <Spinner Android:id= "@+id/spinner"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"/> <ListView Android:id= "@+id/listview"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"/> <GridView Android:id= "@+id/gridview"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"/> </LinearLayout>
Step three: Fix the Baseadapterdemo.java code as follows:
PackageCom.tutor.baseadapter; Importandroid.app.Activity; ImportAndroid.graphics.Color; ImportAndroid.os.Bundle; ImportAndroid.view.View; ImportAndroid.view.ViewGroup; ImportAndroid.widget.BaseAdapter; ImportAndroid.widget.GridView; ImportAndroid.widget.ListView; ImportAndroid.widget.Spinner; ImportAndroid.widget.TextView; Public classBaseadapterdemoextendsActivity {PrivateSpinner Mspinner; PrivateListView Mlistview; PrivateGridView Mgridview; PrivateMyadapter Mmyadapter; @Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.main); Setupviews (); } Public voidsetupviews () {Mmyadapter=NewMyadapter (); Mspinner=(Spinner) Findviewbyid (R.id.spinner); Mspinner.setadapter (Mmyadapter); Mlistview=(ListView) Findviewbyid (R.id.listview); Mlistview.setadapter (Mmyadapter); Mgridview=(GridView) Findviewbyid (R.id.gridview); Mgridview.setadapter (Mmyadapter); Mgridview.setnumcolumns (2); } //define your own adapters, note the GetCount and GetView methods Private classMyadapterextendsbaseadapter{@Override Public intGetCount () {//here I go back to 10, which is a total of 10 items of data return10; } @Override PublicObject GetItem (intarg0) { returnarg0; } @Override Public LongGetitemid (intposition) { returnposition; } @Override PublicView GetView (intposition, View Convertview, ViewGroup parent) { //position is the location starting from 0, Convertview is the view that each item in the Spinner,listview to display//usually return view is Convertview//The parent is the Father form, which is spinner,listview,gridview. TextView Mtextview =NewTextView (Getapplicationcontext ()); Mtextview.settext ("Baseadapterdemo"); Mtextview.settextcolor (color.red); returnMtextview; } } }
The fourth step: Run the program as follows:
One:
Two:
And so on, usually I am here and everyone farewell, today is not over Oh, because the following is our focus, we usually look at the application list what, not just a textview can be an accident, so we can in layout in advance to define the layout. Here I created a new name Baseadapter_provider.xml file, the code is as follows:
<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"android:orientation= "Horizontal"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent" > <ImageView Android:id= "@+id/imageview"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:src= "@drawable/icon"/> <TextView Android:id= "@+id/textview"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Baseadapter"/> </LinearLayout>
Modify the GetView () method as follows:
@Override PublicView GetView (intposition, View Convertview, ViewGroup parent) { //position is the location starting from 0, Convertview is the view that each item in the Spinner,listview to display//usually return view is Convertview//The parent is the Father form, which is spinner,listview,gridview. //TextView Mtextview = new TextView (Getapplicationcontext ()); //mtextview.settext ("Baseadapterdemo"); //Mtextview.settextcolor (color.red); //return mtextview; //Layoutinflater won't refer to my Android Master Advanced Tutorial (v)Convertview =Layoutinflater.from (Getapplicationcontext ()). Inflate (R.layout.baseadapter_provider,NULL); TextView Mtextview=(TextView) Convertview.findviewbyid (R.id.textview); Mtextview.settext ("Baseadapterdemo" +position); Mtextview.settextcolor (color.red); returnConvertview; }
Run again to see the following:
Advanced Android Tutorials (16)---The Use of baseadapter (Spinner,listview,gridview) in Android!