"Go" baseadapter usage

Source: Internet
Author: User

Transferred from: http://blog.csdn.net/wangjia55/article/details/7430759

Hello everyone, today to explain to you the use of Android Baseadapter (base adapter), the role of the adapter is mainly used for such as (Spinner, ListView, GridView) to populate the data. and (Spinner, ListView, GridView) have their own adapters (remember trouble). But Baseadapter (a trick) is universal to them, why do you say so, first we look at the API documentation:

We see that Android Baseadapter has implemented ListAdapter and Spinneradapter interfaces, and the GridView adapter is a ListAdapter interface, but two-dimensional. So baseadapter is common to all three of them.

Let me take a look at the main usage of baseadapter is that 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.

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"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"android:orientation= "vertical"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent">    <TextViewAndroid:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:text= "Welcome to Mr Wei ' s Blog" />    <SpinnerAndroid:id= "@+id/spinner"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"        />    <ListViewAndroid:id= "@+id/listview"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"        />    <GridViewAndroid: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:

Here is our focus, the list of applications we normally look at, 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"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"android:orientation= "Horizontal"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent">    <ImageViewAndroid:id= "@+id/imageview"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:src= "@drawable/icon"        />    <TextViewAndroid: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:

"Go" baseadapter usage

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.