Android advanced tutorial (16)-Use of the omnipotent baseadapter (spinner, listview, and gridview) in Android!

Source: Internet
Author: User

Hello everyone! Today, I want to explain how to use baseadapter (Basic adapter). The adapter is mainly used to fill data for (such as spinner, listview, and gridview. While (spinner, listview, and gridview) all have their own adapters (difficult to remember ). However, baseadapter is common to them. Why? First, let's take a look at the API documentation:

We can see that baseadapter has implemented the listadapter and spinneradapter interfaces, while the gridview adapter implements the listadapter interface, which is only two-dimensional. Therefore, baseadapter is common for the three of them.

 

The following describes the main usage of baseadapter. we define a class (for example, myadapter) and this class inherits baseadapter. because it is implements interfaces of listadapter and spinneradapter, the code for implementing the method in is as follows (without any changes ):

 

Private class myadapter extends baseadapter {<br/> @ override <br/> Public int getcount () {<br/> // todo auto-generated method stub <br/> return 0; <br/>}< br/> @ override <br/> Public object getitem (INT arg0) {<br/> // todo auto-generated method stub <br/> return NULL; <br/>}< br/> @ override <br/> Public long getitemid (INT position) {<br/> // todo auto-generated method stub <br/> return 0; <br/>}< br/> @ override <br/> Public View getview (INT position, view convertview, viewgroup parent) {<br/> // todo auto-generated method stub <br/> return NULL; <br/>}</P> <p>}

 

To make it easier for everyone to understand, write a simple demo by using old rules.

 

Step 1: Create an android project named baseadapterdemo.

 

Step 2: Modify the main. XML Code as follows:

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: Orientation = "vertical" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent" <br/> <textview <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "wrap_content" <br/> Android: TEXT = "welcome to Mr Wei's blog" <br/> <spinner <br/> Android: id = "@ + ID/Spinner" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "wrap_content" <br/> <listview <br/> Android: Id = "@ + ID/listview" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "wrap_content" <br/> <gridview <br/> Android: id = "@ + ID/gridview" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "wrap_content" <br/> </linearlayout>

Step 3: Modify the baseadapterdemo. Java code as follows:

Package COM. tutor. baseadapter; <br/> Import android. app. activity; <br/> Import android. graphics. color; <br/> Import android. OS. bundle; <br/> Import android. view. view; <br/> Import android. view. viewgroup; <br/> Import android. widget. baseadapter; <br/> Import android. widget. gridview; <br/> Import android. widget. listview; <br/> Import android. widget. spinner; <br/> Import android. widget. textview; <br/> public class baseadapterdemo extends activity {</P> <p> private spinner mspinner; <br/> private listview mlistview; <br/> private gridview mgridview; <br/> private myadapter mmyadapter; <br/> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. main); <br/> setupviews (); <br/>}</P> <p> Public void setupviews () {<br/> mmyadapter = new myadapter (); <br/> mspinner = (spinner) findviewbyid (R. id. spinner); <br/> mspinner. setadapter (mmyadapter); <br/> mlistview = (listview) findviewbyid (R. id. listview); <br/> mlistview. setadapter (mmyadapter); <br/> mgridview = (gridview) findviewbyid (R. id. gridview); <br/> mgridview. setadapter (mmyadapter); <br/> mgridview. setnumcolumns (2); </P> <p >}</P> <p> // define your own adapter, note the getcount and getview methods <br/> private class myadapter extends baseadapter {<br/> @ override <br/> Public int getcount () {<br/> // here, 10 is returned, that is, a total of 10 data items <br/> return 10; <br/>}< br/> @ override <br/> Public object getitem (INT arg0) {<br/> return arg0; <br/>}< br/> @ override <br/> Public long getitemid (INT position) {<br/> return position; <br/>}< br/> @ override <br/> Public View getview (INT position, view convertview, viewgroup parent) {<br/> // position indicates that the position starts from 0, and convertview is a spinner, view to be displayed in each item of listview <br/> // usually the return view is convertview <br/> // parent is the parent form, that is, the spinner, listview, gridview. <br/> textview mtextview = new textview (getapplicationcontext (); <br/> mtextview. settext ("baseadapterdemo"); <br/> mtextview. settextcolor (color. red); <br/> return mtextview; <br/>}</P> <p >}< br/>}

 

Step 4: run the program as follows:

I:

 

II:

 

 

Wait, I usually say goodbye to everyone here. Today is not over yet, because the following is our focus. What is the list of applications that we usually look, it is not just a textview, so we can define the layout in layout. Here I created a new file named baseadapter_provider.xml with the following code:

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: Orientation = "horizontal" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent" <br/> <imageview <br/> Android: id = "@ + ID/imageview" <br/> Android: layout_width = "wrap_content" <br/> Android: layout_height = "wrap_content" <br/> Android: src = "@ drawable/icon" <br/> <textview <br/> Android: id = "@ + ID/textview" <br/> Android: layout_width = "wrap_content" <br/> Android: layout_height = "wrap_content" <br/> Android: TEXT = "baseadapter" <br/> </linearlayout> <br/>

 

Modify the getview () method as follows:

@ Override <br/> Public View getview (INT position, view convertview, viewgroup parent) {<br/> // position indicates that the position starts from 0 and convertview is a spinner, view to be displayed in each item of listview <br/> // usually the return view is convertview <br/> // parent is the parent form, that is, the spinner, listview, gridview. <br/> // textview mtextview = new textview (getapplicationcontext (); <br/> // mtextview. settext ("baseadapterdemo"); <br/> // mtextview. settextcolor (color. red); <br/> // return mtextview; </P> <p> // layoutinflater cannot refer to my advanced android tutorial (5) <br/> convertview = layoutinflater. from (getapplicationcontext ()). inflate <br/> (R. layout. baseadapter_provider, null); </P> <p> textview mtextview = (textview) convertview. findviewbyid (R. id. textview); <br/> mtextview. settext ("baseadapterdemo" + position); <br/> mtextview. settextcolor (color. red); <br/> return convertview; <br/>}

Run again to see the following:

 

Okay, you have done it. Well, here I am going to say goodbye to you. If you have any questions, leave a message and leave an email address for the source code. THX ~

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.