Use of the omnipotent baseadapter (spinner, listview, and gridview) in Android!

Source: Internet
Author: User

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
  1. Private class myadapter extends baseadapter {
  2. @ Override
  3. Public int getcount (){
  4. // Todo auto-generated method stub
  5. Return 0;
  6. }
  7. @ Override
  8. Public object getitem (INT arg0 ){
  9. // Todo auto-generated method stub
  10. Return NULL;
  11. }
  12. @ Override
  13. Public long getitemid (INT position ){
  14. // Todo auto-generated method stub
  15. Return 0;
  16. }
  17. @ Override
  18. Public View getview (INT position, view convertview, viewgroup parent ){
  19. // Todo auto-generated method stub
  20. Return NULL;
  21. }
  22. }

 

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
  1. <? XML version = "1.0" encoding = "UTF-8"?>
  2. <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
  3. Android: Orientation = "vertical"
  4. Android: layout_width = "fill_parent"
  5. Android: layout_height = "fill_parent"
  6. >
  7. <Textview
  8. Android: layout_width = "fill_parent"
  9. Android: layout_height = "wrap_content"
  10. Android: text = "welcome to Mr Wei's blog"
  11. />
  12. <Spinner
  13. Android: Id = "@ + ID/Spinner"
  14. Android: layout_width = "fill_parent"
  15. Android: layout_height = "wrap_content"
  16. />
  17. <Listview
  18. Android: Id = "@ + ID/listview"
  19. Android: layout_width = "fill_parent"
  20. Android: layout_height = "wrap_content"
  21. />
  22. <Gridview
  23. Android: Id = "@ + ID/gridview"
  24. Android: layout_width = "fill_parent"
  25. Android: layout_height = "wrap_content"
  26. />
  27. </Linearlayout>

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

[Java]
View plaincopy
  1. Package com. Tutor. baseadapter;
  2. Import Android. App. activity;
  3. Import Android. Graphics. color;
  4. Import Android. OS. Bundle;
  5. Import Android. View. view;
  6. Import Android. View. viewgroup;
  7. Import Android. widget. baseadapter;
  8. Import Android. widget. gridview;
  9. Import Android. widget. listview;
  10. Import Android. widget. spinner;
  11. Import Android. widget. textview;
  12. Public class baseadapterdemo extends activity {
  13. Private spinner mspinner;
  14. Private listview mlistview;
  15. Private gridview mgridview;
  16. Private myadapter mmyadapter;
  17. @ Override
  18. Public void oncreate (bundle savedinstancestate ){
  19. Super. oncreate (savedinstancestate );
  20. Setcontentview (R. layout. Main );
  21. Setupviews ();
  22. }
  23. Public void setupviews (){
  24. Mmyadapter = new myadapter ();
  25. Mspinner = (spinner) findviewbyid (R. Id. spinner );
  26. Mspinner. setadapter (mmyadapter );
  27. Mlistview = (listview) findviewbyid (R. Id. listview );
  28. Mlistview. setadapter (mmyadapter );
  29. Mgridview = (gridview) findviewbyid (R. Id. gridview );
  30. Mgridview. setadapter (mmyadapter );
  31. Mgridview. setnumcolumns (2 );
  32. }
  33. // Define your own Adapter. Note the getcount and getview methods.
  34. Private class myadapter extends baseadapter {
  35. @ Override
  36. Public int getcount (){
  37. // Here I return 10, which is a total of 10 data items.
  38. Return 10;
  39. }
  40. @ Override
  41. Public object getitem (INT arg0 ){
  42. Return arg0;
  43. }
  44. @ Override
  45. Public long getitemid (INT position ){
  46. Return position;
  47. }
  48. @ Override
  49. Public View getview (INT position, view convertview, viewgroup parent ){
  50. // Position indicates that the position starts from 0 and convertview is a spinner.
  51. // Usually the return view is convertview
  52. // Parent is the parent form, that is, the spinner, listview, and gridview.
  53. Textview mtextview = new textview (getapplicationcontext ());
  54. Mtextview. settext ("baseadapterdemo ");
  55. Mtextview. settextcolor (color. Red );
  56. Return mtextview;
  57. }
  58. }
  59. }

 

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
  1. <? XML version = "1.0" encoding = "UTF-8"?>
  2. <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
  3. Android: Orientation = "horizontal"
  4. Android: layout_width = "fill_parent"
  5. Android: layout_height = "fill_parent"
  6. >
  7. <Imageview
  8. Android: Id = "@ + ID/imageview"
  9. Android: layout_width = "wrap_content"
  10. Android: layout_height = "wrap_content"
  11. Android: src = "@ drawable/icon"
  12. />
  13. <Textview
  14. Android: Id = "@ + ID/textview"
  15. Android: layout_width = "wrap_content"
  16. Android: layout_height = "wrap_content"
  17. Android: text = "baseadapter"
  18. />
  19. </Linearlayout>

 

Modify the getview () method as follows:

[Java]
View plaincopy
  1. @ Override
  2. Public View getview (INT position, view convertview, viewgroup parent ){
  3. // Position indicates that the position starts from 0 and convertview is a spinner.
  4. // Usually the return view is convertview
  5. // Parent is the parent form, that is, the spinner, listview, and gridview.
  6. // Textview mtextview = new textview (getapplicationcontext ());
  7. // Mtextview. settext ("baseadapterdemo ");
  8. // Mtextview. settextcolor (color. Red );
  9. // Return mtextview;
  10. // Layoutinflater does not refer to my advanced android tutorial (5)
  11. Convertview = layoutinflater. From (getapplicationcontext (). Inflate
  12. (R. layout. baseadapter_provider, null );
  13. Textview mtextview = (textview) convertview. findviewbyid (R. Id. textview );
  14. Mtextview. settext ("baseadapterdemo" + position );
  15. Mtextview. settextcolor (color. Red );
  16. Return convertview;
  17. }

Run again to see the following:

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.