Simple use of Baseadapter in Android

Source: Internet
Author: User

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 if you use baseadapter directly, don't worry about it, first look at the general introduction of the API

These will know why Baseadapter is a fresh move, because it is realized adapter,listadapter,spinneradapter. Next look at a number of methods that inherit the Baseadapter class to override:

 PackageCom.example.baseadapterforgridview;Importandroid.app.Activity;ImportAndroid.graphics.Color;ImportAndroid.os.Bundle;ImportAndroid.view.LayoutInflater;ImportAndroid.view.View;ImportAndroid.view.ViewGroup;ImportAndroid.widget.BaseAdapter;ImportAndroid.widget.GridView;ImportAndroid.widget.TextView; Public classMainactivityextendsActivity {PrivateGridView Mgridview; PrivateMbaseadapter Baseadapter; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); Baseadapter=NewMbaseadapter (); Mgridview=(GridView) Findviewbyid (R.id.gridview);                    Mgridview.setadapter (Baseadapter); }    classMbaseadapterextendsbaseadapter{@Override Public intGetCount () {//TODO auto-generated Method Stub            return42; } @Override PublicObject GetItem (intposition) {            //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 StubLayoutinflater inflater = Layoutinflater.from (mainactivity. This); View MView= Inflater.inflate (R.layout.item,NULL);//TextView Mtextview = new TextView (Getapplicationcontext ());//mtextview.settext ("Baseadapterdemo");//Mtextview.settextcolor (color.red);            returnMView; }            }     }

The GridView XML file:

<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:paddingbottom= "@dimen/activity_vertical_margin"Android:paddingleft= "@dimen/activity_horizontal_margin"Android:paddingright= "@dimen/activity_horizontal_margin"Android:paddingtop= "@dimen/activity_vertical_margin"Tools:context= "Com.example.baseadapterforgridview.MainActivity" > <GridView Android:id= "@+id/gridview"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:stretchmode= "ColumnWidth"Android:columnwidth= "120dip"android:verticalspacing= "10dip"android:horizontalspacing= "10dip"Android:cachecolorhint= "@android: Color/transparent"Android:numcolumns= "Auto_fit" > </GridView></RelativeLayout>

Each item inside the layout file:

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "Vertical" > <TextView Android:id= "@+id/text"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Texviewtest"/> <Button Android:id= "@+id/button"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Test"/></linearlayout>

Effect of execution:

As you can see, each item contains two elements, one textview content and one button.

What is commented out in the GetView () method is a way to "load" the layout file, while using layoutinflater~~ is another way to load the XML, and the benefit is to load the layout files that have already been defined in the XML and use them directly.

OK, this is a simple baseadapter use demo, if wrong, hope to correct. Thank you

Simple use of Baseadapter in Android

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.