Android's Custom implementation Baseadapter (Universal Adapter II) _android

Source: Internet
Author: User

In the last general adapter one, we have taken viewholder to a generic holding class, which has greatly reduced our writing to code, and is now starting to extract it on that basis to achieve better quality. First we review the code in the Myadapter.java class in the previous article, and then we extract the encapsulation again through this code.

public class Myadapter extends Mybaseadapter {public
  myadapter (list<student> data) {
    super (data);
  }
  @Override public
  View getview (int position, View Convertview, ViewGroup parent) {
    //Get Holder Hold object
    Viewholder holder = Viewholder.getholder (convertview,parent,position, r.layout.list_item);
    Gets all the controls in the holder object
    TextView tvname = Holder.getview (R.ID.MTV1);
    The control is manipulated to implement the corresponding method
    TextView tvsex = Holder.getview (R.ID.MTV2);
    Tvname.settext (Data.get (position). GetName ());
    Tvsex.settext (Data.get (position). Getsex ());
    Returns the corresponding layout return
    Holder.getconvertview ();
  }

From the above we can observe again, which code is our format does not change, or form is almost all duplicate it? The answer is, of course, if you write this myadapter every time, you will find that in GetView, you will get holder object Viewholder holder = Viewholder.getholder ( Convertview,parent,position, R.layout.list_item), and then, in addition to each time the function of the control is different (such as text set text, picture control settings picture, etc.), the last returned statement return Holdergetconvertview () is it the same? So we can extract the same form of code at one time, according to the object-oriented knowledge, we can extract the common function into the parent class, and then give the specific details that need to be implemented to the subclass to implement, that is, the template method design mode, the following start to extract the code into his parent class

Mybaseadapter.java

Public abstract class Mybaseadapter extends Baseadapter {
  protected list<student> data;
  Public Mybaseadapter (list<student> data) {
    this.data = data;
  }
  @Override public
  int GetCount () {return
    data = null 0:data.size ();
  }

  @Override public
  Object getitem (int position) {return
    data.get (position);
  }

  @Override public
  long getitemid (int position) {return
    position;
  }

  @Override public
  View getview (int position, View Convertview, ViewGroup parent) {
    //the same implementation is extracted to the parent class, here is fixed
    Viewholder holder = Viewholder.getholder (convertview,parent,position, r.layout.list_item);
    Then provide a concrete implementation method for subclasses
    SetData (holder,data.get (position));
    This is also the same return code, which is also a fixed returning
    holder.getconvertview ();
  }
  This method is set to abstract so that his subclass must implement the public
  abstract void SetData (Viewholder holder,t T);
}

Finally, take a look at the code inside our Myadapter.java:

public class Myadapter extends Mybaseadapter {public
  myadapter (list<student> data) {
    super (data);
  }
  @Override public
  void SetData (Viewholder holder, Student t) {
    TextView tvname = Holder.getview (R.ID.MTV1);
    Tvname.settext (T.getname ());
    TextView tvsex = Holder.getview (R.ID.MTV2);
    Tvsex.settext (T.getsex ());
  }


You will find that in the end, your custom myadapter only needs to implement this code, and each time you just need to put the perfect parent class Mybaseadaper.java and the generic Viewholder class in your corresponding package, After no custom myadapter only need to write this code, code perfect here to believe that for beginners has been so little excited, and finally we can improve the code, that is, to optimize the code into a universal adapter, then what does this mean? Sometimes you find yourself writing code here in SetData, if the control has 20, and then each time you get the control, set the corresponding value, you will not find that the moment this code more, so in order to let this code can be optimized point, the specific implementation in the next article in the completion.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.