Java Generic Operations Review and explain the scenarios used in Android

Source: Internet
Author: User
Tags addall tojson

There are many places where Android uses generics, such as the integrated self-baseadapter implementation of encapsulated adapter, which encapsulates common operations, but requires processing of incoming data, using generics at this time, as shown in the following example:

 Public Abstract classEasyadapter<t>extendsBaseadapter {PrivateLayoutinflater Inflater; Private intLayoutID; PrivateList<t> mlist =NewArraylist<t>();  PublicEasyadapter (Context context,intLayoutID, list<t>list) {        Super();  This. Inflater =Layoutinflater.from (context);  This. LayoutID =LayoutID;  This. mlist =list; }    /*** Add data to the top * *@paramList*/     Public voidAdd2head (list<t>list) {Mlist.addall (0, list);    Notifydatasetchanged (); }         Public voidClearAll () {mlist.clear ();    Notifydatasetchanged (); }     PublicList<t>getalllist () {returnmlist; }    /*** Add data to the bottom * *@paramList*/     Public voidAdd2bottom (list<t>list)        {Mlist.addall (list);    Notifydatasetchanged (); }     Public voidAdd2bottom (T t) {Mlist.add (t);    Notifydatasetchanged (); }    /*** @Title: Updatelistview * @Description: TODO (update data in baseadapter) *@param @paramList settings file *@returnvoid return type *@throws     */     Public voidUpdatelistview (list<t>list) {mlist=list;    Notifydatasetchanged (); } @Override Public intGetCount () {returnmlist.size (); } @Override PublicT GetItem (intposition) {        returnMlist.get (position); } @Override Public LongGetitemid (intposition) {        returnposition; }    /*** The method to actually display the view, using an abstract method to force the caller to overwrite! */@Override PublicView GetView (intposition, View Convertview, ViewGroup parent) {Viewholder Viewholder=Viewholder.getviewholder (parent, Convertview, Inflater, LayoutID);        Convert (Viewholder, mlist.get (position)); returnViewholder.getconvertview (); }     Public Abstract voidconvert (Viewholder viewholder, T t);}

The code comes from my github:https://github.com/soyoungboy/easyanroid/blob/master/easyanroid/src/com/soyoungboy/base/adapter/. Easyadapter.java

There are, for example, parsing Json,json beans each have different:

 Public classGsonimplextendsJson {PrivateGson Gson =NewGson (); @Override PublicString ToJson (Object src) {returnGson.tojson (SRC); } @Override Public<T> T Toobject (String json, class<t>Claxx) {        returnGson.fromjson (JSON, Claxx); } @Override Public<T> T Toobject (byte[] Bytes, class<t>Claxx) {        returnGson.fromjson (NewString (bytes), Claxx); } @Override Public<T> list<t> toList (String json, class<t>Claxx) {Type type=NewTypetoken<arraylist<t>>() {}.gettype (); List<T> list =Gson.fromjson (JSON, type); returnlist; }}

The following focuses on the knowledge points of generics in the Java Foundation:

First: Generics are a new feature provided by java1.5, primarily to address the security of data types, by indicating the type of a property in a class or the return value of a method and the type of the parameter at the time of class declaration. This allows the class to be declared or instantiated as long as the desired type is specified.

Java Generic Operations Review and explain the scenarios used in Android

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.