Android advanced_ Third party framework xutils

The functions of xutils mainly include four parts: (1) layout view Association; (2) Picture downloading and caching; (3) Network request; (4) Database;

1. Use xutils for view injection:

(1) Add @ viewinject() above the control declaration to pass in the resource ID of the control;

(2) Use x.view(). Inject() in oncreate() to pass in the context object;

(3) Set value click event:

(3-1) user defined click event method; The access permission is private void, and the view parameter is passed in;

(3-2) add the annotation @ event () above the method to the resource ID of the incoming control;

Note: if multiple controls are set in the same click event, pass in the int array in @ event(), and the array can contain different control resource IDS;

(3-3) the second parameter passed in @ event () is the event type. The default type is onclicklistener.class, which can be modified to type = adapterview.onitemclicklistener.class;

3. Online picture download:

(1) Add network access permissions, read and write external memory permissions;

(2) Declare and instantiate the ImageView control;

(3) Initialize the xuutils network request in oncreate() of activity or application: call x.ext.init (getapplication());

(3-1) in the custom class that inherits application, oncreate() uses:

(3-2) onterminate(), call this method when the program exits;

(3-3) onlowmemeory(), call this method when the program running memory is insufficient;

(4) Set picture parameters:

(4-1) create an imageoptions.builder object image parameter creation class, and call the following methods respectively;

(4-2) setconfig (bitmap. Config. Rgb_565) set picture parameters;

(4-3) setusememcache (true) sets whether to cache pictures; Note: This is the 3G cache function;

(4-4) setradius (10) set picture fillet; Note: use with caution. It is the processing of images, not the direct operation of ImageView;

(4-5) setloadingdrawableid() sets the image resource ID when loading;

(4-6) setfailuredrawableid() sets the display resource ID when the picture download fails;

(4-7) setignoregif() sets whether to ignore GIF format pictures; False indicates dynamic display;

(4-8) the imageoptions.builder object calls build() to return the imageoptions object and start the created picture parameter object;

(5) Start image download, call x.image(). Bind() to pass in two or three parameters:

Parameter 1: ImageView control to be displayed;

Parameter 2: image download path;

Parameter 3: (optional) image parameters of imageoptions object;

ImageView image loading process: at first, the image will be loaded from memory. If the image is found, the image will be returned directly. If not, the image will be fetched from the hard disk cache. If the hard disk cache is not available, the image will be loaded from the network; When a picture is loaded from the network, one will be saved in the memory and one will be saved in the external sdcard;

4. Network request

(1) Get download request:

(1-1) add network access permissions, read and write external memory permissions;

(1-2) set request parameters: create a requestparams object and transfer it to the network download path;

(1-3) start downloading data: x.http(). Get(), pass in two parameters and return the callback.cancellable object:

Parameter 1: requestparams object;

Parameter 2: object implementing callback. Commoncallback < string > interface;

(1-4) implement callback. Commoncallback < string > interface: Rewrite four methods:

Method 1:onSuccess (String result): this method is called after successful download.

Method 2: onerror(): call this method when the download fails;

Method 3: oncancelled(): call this method when canceling the download;

Method 4: onfinished(): call this method at the end of the download;

(1-5) in ondestroy(), if it is judged that the cancelable object is not empty and can be cancelled, cancel the network download task: the cancelable object can call cancel();

(2) Post upload request:

(2-1) add network access permission, read and write external memory permission in the list file;

(2-2) set request parameters: create a requestparams object and transfer it to the network upload path;

(2-3) the requestparameters object calls addbodyparameter() to pass in data in the form of key value pairs;

(2-4) send request: x.http(). Post(), pass in two parameters and return the callback.cancellable object:

Parameter 1: requestparams object;

Parameter 2: callback listener implements the callback. Commoncallback < string > interface, and overrides four methods in the class that implements the interface:

Method 1: onsuccess(): call back this method when the upload is successful;

Method 2: onerror(): call back this method when upload fails;

Method 3: oncancelled(): call back this method when uploading is cancelled;

Method 4: onfinished(): call back this method at the end of upload;

(3) Upload upload file: in the requestparameters object, create an upload file path and put it into the requestparameters object's call addbodyparameter() in the form of key value pairs; Other places are the same as the uploaded data;

(4) Download Download File

(4-1) use the project tools under the download package;

(4-2) create file download path: the environment object calls getexternalstoragedirectory(). Getabsolutepath() to get the absolute path;

(4-3) the downloadmanager object calls getinstance() to get the downloadmanager object;

(4-4) the downloadmanager object calls startdownload() and passes in six parameters:

Parameter 1: download path;

Parameter 2: save tag;

Parameter 3: file path;

Parameter 4: support breakpoint continuation;

Parameter 5: support renaming;

Parameter 6: null null parameter;

5. Database function: add, delete, modify and query can be set, and the database information is stored in the form of module;

(1) Create a JavaBean to save a table information in the database;

Add a comment before the current JavaBean: @ table(), pass in name = "" as the table name;

Add a comment before the attribute in the JavaBean: @ column(), pass in id = "" as the field ID and name = "" as the field name;

Note: there must be an ID attribute in the JavaBean corresponding to the database, and the binding annotation is @ column (ISID = true, name = ""), otherwise a null pointer exception will be reported during operation;

(2) Configure database information in activity:

(2-1) create the dbmanager.daoconfig object;

(2-2) setdbname(), pass in the database name;

(2-3) setdbopenlistener(), pass in the object that implements the dbmanager.dbopenlistener interface, and rewrite ondbopened() to open the data;

(2-4) setdbupgradelistener(), pass in the object implementing the dbmanager.dbupgradelistener interface, and override ondbupgrade() to be used when updating the database;

(3) Add, delete, modify and query where the database needs to be operated:

(3-1) add:

Create JavaBean object and set data; Use x.getDb () to import the DbManager.DaoConfig object created, and then call save () to JavaBean object.

(3-2) delete:

1) delete the entire table: x.getDb () the DbManager.DaoConfig object that is passed in, and then call delete () to JavaBean.class.

2) delete single record according to Id: x.getDb () is passed to create DbManager.DaoConfig object, then call deleteById (), pass in two parameters: JavaBean.class and Id to delete the record;

3) delete a single record according to the condition: x.getDb () is passed to the DbManager.DaoConfig object created, then call delete (), and pass in the two parameters: JavaBean.class and WhereBuilder.b ().And ().Or ().Exo (), and so on.

(3-3) change:

1) modify the single record according to Id: x.getDb () the incoming DbManager.DaoConfig object, then call Update (), and pass in more than two parameters: the JavaBean object created and the field name to be modified;

2) modify records according to the conditions: x.getDb () the DbManager.DaoConfig object that is passed in to create, then call Update (), pass in more than three parameters: JavaBean.class, WhereBuilder.b (), pass in the conditional parameter, and save the KeyValue object corresponding to the modified key value;

(3-4) check:

X. getDb () passes to the DbManager.DaoConfig object created, then calls selector (), passes in the JavaBean.class parameter, returns the Selector<JavaBean> object; After that, the where () is invoked by calling the whereBuilder.b () condition parameter, and then called findAll () to start the search, and the List<JavaBean> object is returned as the result of the search.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>