android--Arrayadapter Usage Example (reprint)

Source: Internet
Author: User

      Many recent android developers letter to arrayadapter and baseadapter the difference is not very clear, here Android123 simply say their relationship and usefulness, Arrayadapter is derived from Baseadapter, With all the features of the Baseadapter, but the arrayadapter is more powerful, it can be instantiated directly using generic constructs, we can see in the Android SDK android.widget.arrayadapter<t> words , of course, you can also use the second parameter of Arrayadapter (context context, int textviewresourceid) to directly bind a layout, the following example we use Java generic instantiation.

By adapter We construct an item that supports icon, the following we use in the GetView is the ImageView display picture, Of course android123 prompt everyone in fact TextView can also be directly bound to a Drawable object display,void  setcompounddrawables (drawable left, drawable top, Drawable right, drawable bottom) or void  setcompounddrawableswithintrinsicbounds (int left, int top, int. right, int bot Tom) and void  Setcompounddrawableswithintrinsicbounds (drawable left, drawable top, drawable right, drawable bottom) Then, where the second type of int specifies the resource ID, the azimuth is TextView where the Drawable object is displayed

said so much arrayadapater together to see an example, to instantiate Arrayadapter, we can modify the Res /layout/icon_list_item.xml files to achieve custom display effects.

Java code
  1. Public class Iconlistadapter extends arrayadapter<iconlistadapter.iconlistitem> {
  2. protected Layoutinflater Minflater;
  3. private static final int mresource = R.layout.icon_list_item; //xml layout file
  4. Public Iconlistadapter (context context,
  5. List<iconlistitem> items) {
  6. Super (context, mresource, items);
  7. Minflater = (layoutinflater) context.getsystemservice (Context.layout_inflater_service);
  8. }
  9. @Override
  10. Public View GetView (int position, View Convertview, ViewGroup parent) {
  11. TextView text;
  12. ImageView image;
  13. View view;
  14. if (Convertview = = null) {
  15. View = Minflater.inflate (Mresource, parent, false);
  16. } Else {
  17. view = Convertview;
  18. }
  19. Text = (TextView) View.findviewbyid (R.ID.TEXT1);
  20. Text.settext (GetItem (position). GetTitle ());
  21. Image = (ImageView) View.findviewbyid (R.id.icon); //can use the above mentioned three methods, directly with the TextView class Setcompounddrawables and other methods of binding icon display
  22. Image.setimageresource (GetItem (position). GetResource ());
  23. return view;
  24. }
  25. public static class Iconlistitem { //construction method for each display
  26. Private final String mtitle;
  27. private final int mresource;
  28. Public Iconlistitem (String title, int Resource) {
  29. Mresource = resource;
  30. Mtitle = title;
  31. }
  32. Public String GetTitle () {
  33. return mtitle;
  34. }
  35. public int getresource () {
  36. return mresource;
  37. }
  38. }
  39. }

Of course for Arrayadapter in the end than Baseadapter advanced to where? From the name array we can contact the array of many operations, yes Android123 gives you a list of all members of this class of practical processing methods, such as:

Java code
  1. void Add (T object) //Add an object to this arrayadapter
  2. void Clear () //Clear all elements
  3. Static arrayadapter<charsequence> Createfromresource (context context, int textarrayresid, int TEXTVIEWRESID) //structure Arrayadapter from layout resources
  4. Context getcontext () //Get instance
  5. int GetCount ()
  6. View Getdropdownview (int position, view Convertview, ViewGroup Parent) //Gets the contents of the popup style selection entry for drop down, parameter 1 is the position, Parameter 2 can obtain the contents of this article directly through casting
  7. Filter GetFilter () //using regular filtering Data
  8. T GetItem (int position) //Get a single piece of content
  9. Long Getitemid (int position)
  10. int getPosition (T Item) //through content fetching is a piece of
  11. View GetView (int position, view Convertview, ViewGroup parent)
  12. void Insert (T object, int index) //Insert new entry to the index position of the array
  13. void notifydatasetchanged () //notification data changed, telling the widget to bind adapter to update the UI
  14. void Remove (T object) //Move out of an array, where there is no specified position
  15. void Setdropdownviewresource (int Resource) //Set layout style for dropdown
  16. Sets the layout resource to create the drop of views.
  17. void Setnotifyonchange (boolean Notifyonchange) //This article is the most powerful feature of Arrayadapter, android123 strongly recommends the use of this method when processing big data, Can reduce the amount of UI processing, refreshing the UI can be faster, mainly to stop the
  18. (Add (t), insert (t, int), remove (t), clear (), of course, can be changed by notifydatasetchanged (). or Setnotifyonchange (true) to notify the change
  19. void sort (comparator<?  Super T> Comparator) //This is the Android development network by the usual sort, using arrayadapter can be directly sorted, very convenient

So finally android123 recommend everyone what situation use Arrayadapter, when to use Baseadapter. When a large number, such as more than 100 or more frequent dynamic increase or decrease in the use of Arrayadapter can easily control the UI, through the Setnotifyonchanage method, if relatively simple rendering directly from the Baseadapter to save resources.

android--Arrayadapter Usage Example (reprint)

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.