Reprinted from: http://blog.csdn.net/android_tutor/article/details/5707835
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 ):
[Java]
View plaincopy
- Private class myadapter extends baseadapter {
- @ Override
- Public int getcount (){
- // Todo auto-generated method stub
- Return 0;
- }
- @ Override
- Public object getitem (INT arg0 ){
- // Todo auto-generated method stub
- Return NULL;
- }
- @ Override
- Public long getitemid (INT position ){
- // Todo auto-generated method stub
- Return 0;
- }
- @ Override
- Public View getview (INT position, view convertview, viewgroup parent ){
- // Todo auto-generated method stub
- Return NULL;
- }
- }
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:
[Java]
View plaincopy
- <? 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 3: Modify the baseadapterdemo. Java code as follows:
[Java]
View plaincopy
- Package com. Tutor. baseadapter;
- Import Android. App. activity;
- Import Android. Graphics. color;
- Import Android. OS. Bundle;
- Import Android. View. view;
- Import Android. View. viewgroup;
- Import Android. widget. baseadapter;
- Import Android. widget. gridview;
- Import Android. widget. listview;
- Import Android. widget. spinner;
- Import Android. widget. textview;
- Public class baseadapterdemo extends activity {
- Private spinner mspinner;
- Private listview mlistview;
- Private gridview mgridview;
- Private myadapter mmyadapter;
- @ Override
- Public void oncreate (bundle savedinstancestate ){
- Super. oncreate (savedinstancestate );
- Setcontentview (R. layout. Main );
- Setupviews ();
- }
- Public void setupviews (){
- Mmyadapter = new myadapter ();
- 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 Adapter. Note the getcount and getview methods.
- Private class myadapter extends baseadapter {
- @ Override
- Public int getcount (){
- // Here I return 10, which is a total of 10 data items.
- Return 10;
- }
- @ Override
- Public object getitem (INT arg0 ){
- Return arg0;
- }
- @ Override
- Public long getitemid (INT position ){
- Return position;
- }
- @ Override
- Public View getview (INT position, view convertview, viewgroup parent ){
- // Position indicates that the position starts from 0 and convertview is a spinner.
- // Usually the return view is convertview
- // Parent is the parent form, that is, the spinner, listview, and gridview.
- Textview mtextview = new textview (getapplicationcontext ());
- Mtextview. settext ("baseadapterdemo ");
- Mtextview. settextcolor (color. Red );
- Return mtextview;
- }
- }
- }
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:
[Java]
View plaincopy
- <? 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:
[Java]
View plaincopy
- @ Override
- Public View getview (INT position, view convertview, viewgroup parent ){
- // Position indicates that the position starts from 0 and convertview is a spinner.
- // Usually the return view is convertview
- // Parent is the parent form, that is, the spinner, listview, and gridview.
- // Textview mtextview = new textview (getapplicationcontext ());
- // Mtextview. settext ("baseadapterdemo ");
- // Mtextview. settextcolor (color. Red );
- // Return mtextview;
- // Layoutinflater does not refer to my advanced android tutorial (5)
- 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 );
- Return convertview;
- }
Run again to see the following: