The adapter of Dhroid-netjsonadapter network

Source: Internet
Author: User

About adapter I think for everyone is no stranger, the basic application will be used a lot, do not know now you are still in accordance with a certain routines to write a lot of code to achieve adapter
I think most people still write a adapter inherited from Baseadapter implementation GetView method (I think we will use the Internet popular Viewholder mode bar),
If the data comes from the network using a network access tool to access the data, the data is converted and then added into the adapter, and if there is a pull-down refresh, loading more code will be more
Problem
1. You are writing a lot of duplicate code
2. Data management is not so easy
3. The network cache needs to be processed

If you use the Dhroid framework, these problems can be well solved
First look at the following code

Netjsonadapter adapter=NewNetjsonadapter ("Http://shishangquan.017788.com/mobile_ordermeal_jujiList", This, R.layout.adapter_item);//Adding ParametersAdapter.addparam ("Key1", "Key1");//Data BindingAdapter.addfield ("username", R.id.name); Adapter.addfield ("Title", r.id.title);//data binding for text decorationAdapter.addfield ("pubdate", R.id.time, "Time");//data binding for picture retouchingAdapter.addfield ("User_faceimg", R.id.pic, "round");//Caching PoliciesAdapter.usecache (Cachepolicy.policy_cache_andrefresh);//RefreshAdapter.refresh ();//bind to the ListViewListv.setadapter (adapter);//This code is for network access, generating views, data binding, caching policies, and more.

Will generate the view through R.layout.adapter_item, where the generated view uses a reuse mechanism, and viewholder mode (I don't feel viewholder useful)


Suppose the result of the network return is

{success:true, data:[{username:' Vine ', title:' Everybody good ', pubdate: 1394707561, user_faceimg:' avatar path '},{}]}


Netjsonadapter will parse the data automatically

Binds username to TextView with ID r.id.name
Will bind pubdate to the TextView with ID r.id.time, where data conversion is required, the value obtained is 1394707561, and the last display is of course 2012-11-08 we wrote the last parameter as time, The specific conversion is written to the Valuefix interface (said below)
Will user_faceimg get to the picture path bound to R.id.pic ImageView, here also made a conversion, is the picture truncated, we in the last parameter is written as round, the specific conversion is written to the Valuefix interface (said below)
Adapter.usecache (Cachepolicy.policy_cache_andrefresh); cache policy can be done, the specific caching strategy everyone, look at Dhnet's documentation.

Adapter.refresh (); refresh


Let's start with network-related

Add parameter Adapter.addparam ("Key1", "Key1"); // Load Next page adapter.shownext (), Load next page with Dialog Adapter.shownextindialog (); Cache policy (View dhnet document) Adapter.usecache ( Cachepolicy.policy_cache_andrefresh); Is there a dialog when loading the first page adapter.showprogressonfrist (true

Data processing related
which node to use as data (support point segmentation)

Adapter.fromwhat ("AAA.BBB");//If your results are not on a node but need to be processed.Adapter.setdatabulider (NewDatabulider () {@Override PublicJsonarray ondate (Response Response) {returnResponse.jsonarrayfrom ("xxx");}}); Adapter.addfield ("Title", r.id.title);//data binding for text decorationAdapter.addfield ("pubdate", R.id.time, "Time");//data binding for picture retouchingAdapter.addfield ("User_faceimg", R.id.pic, "round"); If you need to control the display and hide the Adapter.addfield (NewFieldMap ("Activeaddress", R.id.content) {@Override Publicfix for object (View itemv, Integer po, Object o, Object Jo) {Jsonobject joo=(Jsonobject) Jo;//you can do some extra work here.Itemv.findviewbyid (R.id.icon). Setvisibility (Jsonutil.getint (Joo, "status") ==1?View.VISIBLE:View.INVISIBLE);returno;});

Event callbacks are processed at this time after each load is completed

//callback after successful loadingAdapter.setonloadsuccess (NewLoadsuccesscallback () {@Override Public voidCallBack (Response Response) {if(Response.issuccess ()) {Dialoger.showtoastshort (), getactivity (),"Load succeeded");if(Adapter.getpageno () ==1) {listv.setselection (0);}}}}); Internal Click events//Internal Click eventsAdapter.setoninviewclicklistener (R.id.pic,NewBeanadapter.inviewclicklistener () {@Override Public voidOnclicklistener (view itemv, view V, Integer po,object Jo) {jsonobject joo=(Jsonobject) Jo;dialoger.showtoastlong (Getactivity (), Jsonutil.getstring (Joo,"Username"));}});

Adapter constants that need to be configured (configured in application)

// Paging Parameters Const.netadapter_page_no = "P"; // the length of the paging parameter Const.netadapter_step = "Step"; // Default Paging length Const.netadapter_step_default = 7; // Timeline parameters (key passed in to the background) Const.netadapter_timeline = "Timeline"; // which field does the timeline take Const.netadapter_json_timeline= "Pubdate"

About the timeline refers to which property of the last previous bar is used each time as the parameter for the next access

Let's talk about the data modification issues mentioned above.

Data retouching requires that the class that implements the Valuefix interface be configured in the IOC
The interface has the following methods

If it is textview and the child class is called

 Public Abstractobject Fix (Object obj, String s); The first parameter passed in is the original value of the second parameter type as implemented as @override Publicobject Fix (Object o, String type) {if(O = =NULL)return NULL;if("Time". Equals (Type) {returnGetstandardtime (Long.parselong (o.tostring ()) * 1000,"Yyyy-mm-dd");}returno;} The incoming is 1394707561 and' Time ' is returned in 2014-11-08if it's ImageView, Public Abstractdisplayimageoptions imageoptions (String s); The type round returned is Displayimageoptionsdisplayimageoptions It's Universalimageloader.jar we're using a picture loading is it

I also want to explain the inheritance of the netjsonadapter.
Netjsonadapter inherits the Beanadapter class and implements the Inetadapter interface.
Beanadapter good management of data and view binding implementation
public abstract void BindView (view view, int i, Object obj);
Can be modeled after Netjsonadapter's BindView

Inetadapter defines a network-related approach

 PublicString Gettag (); Public voidrefresh (); Public voidsetonloadsuccess (Loadsuccesscallback loadsuccesscallback); Public voidremoveonloadsuccess (Loadsuccesscallback loadsuccesscallback); Public voidsetontemploadsuccess (Loadsuccesscallback loadsuccesscallback); PublicBoolean Hasmore (); Public voidShownext ();//Public Boolean isloding (); Public voidShownextindialog ();

This enables network operation based on the type of adapter in the implemented ListView

If your list item is particularly complex I think you'd better write a myadapter inherit from Netjsonadapter, re-implement GetView method

The adapter of Dhroid-netjsonadapter network

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.