Android Advanced _ Third party framework Xutils

Source: Internet
Author: User

The functions of xutils mainly include four parts: (1) Layout View Association, (2) Picture download and cache, (3) network request; (4) database;

1. Use Xutils for view injection:

(1) Add the resource ID of the @viewinject () incoming control above the control declaration;

(2) Use X.view (). inject () in OnCreate () to pass in the context object;

(3) Set Value Click event:

(3-1) The custom Click event method; Access rights are private privately void, passing in the view parameter;

(3-2) Add note @event () to the resource ID of the control passed in above the method;

      Note : If multiple controls are set in the same click event, an int array is passed in @event (), and the array is a different resource ID for the control;

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

3. Network Image download:

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

(2) Declare and instantiate the ImageView control;

(3) Initialize the Xutils network request in activity or application OnCreate (): Call X. Ext.init (Getapplication ());

(3-1) in a custom class that inherits application, OnCreate () uses:

(3-2) Onterminate (), calling this method when the program exits;

(3-3) Onlowmemeory (), calling this method when the program runs out of memory;

(4) Set the picture parameters:

(4-1) Create Imageoptions.builder object picture parameter Create class, call method separately;

(4-2) Setconfig (Bitmap.Config.RGB_565) set the picture parameters;

(4-3) Setusememcache (true) sets whether the picture is cached; Note : Here is the 3g cache function;

(4-4) Setradius (10) Set the picture fillet; Note : The use of caution, is the processing of pictures, not the direct operation of ImageView;

(4-5) Setloadingdrawableid () sets the image resource ID at load time;

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

(4-7) Setignoregif () sets whether to ignore pictures in GIF format; false for dynamic display;

(4-8) The Imageoptions.builder object calls build (), returns the Imageoptions object, launches the created picture parameter object;

(5) Start image download, call X.image (). Bind () passes in two or three parameters:

Parameter 1: The ImageView control to display;

Parameter 2: Picture download path;

Parameter 3: (optional) The picture parameter of the Imageoptions object;

ImageView Loading Picture process: The beginning will be loaded from memory in the image, if you find the image will be directly returned to the picture, if not it will be from the hard disk cache inside to take pictures, if the hard disk cache has not been loaded from the network image; loading pictures from the network, will save a copy in memory, At the same time save a copy of the sdcard in the deposit;

4. Network requests

(1) Get download request:

(1-1) Add Network access rights, read and write external memory permissions;

(1-2) Set request parameters: Create Requestparams object, incoming network download path;

(1-3) Start download data: X.http (). Get (), pass in two parameters, return the Callback.cancelable object:

Parameter 1:requestparams object;

Parameter 2: The object that implements the Callback.commoncallback<string> interface;

(1-4) Implement callback.commoncallback<string> interface: Rewrite four methods:

Method 1:onsuccess (String result): This method is called after a successful download;

Method 2:onerror (): This method is called when the download fails;

Method 3:oncancelled (): This method is called when the download is canceled;

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

(1-5) in OnDestroy (), to determine that the Cancelable object is not empty, and can be canceled, cancel the network download task: Cancelable object Call Cancel () can be;

(2) Post upload request:

(2-1) Add Network access rights to the manifest file, read and write external memory permissions;

(2-2) Set request parameters: Create Requestparams object, incoming network upload path;

(2-3) The Requestparams object calls the Addbodyparameter (), passing in the data of the key-value pairs;

(2-4) Send a request: X.http (). Post (), pass in two parameters, return the Callback.cancelable object:

Parameter 1:requestparams object;

Parameter 2: Callback listener implements the Callback.commoncallback<string> interface, overriding four methods in the class that implements the interface:

Method 1:onsuccess (): Callback This method when upload is successful;

Method 2:onerror (): Callback This method when upload fails;

Method 3:oncancelled (): Callback This method when the upload is canceled;

Method 4:onfinished (): Callback This method at the end of upload;

(3) Upload upload file: In the Requestparams object, create the upload file path, in the form of key-value pairs into the Requestparams object call Addbodyparameter (), and other places with the same upload data;

(4) Download Download file

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

(4-2) Create file Download path: Environment object calls getExternalStorageDirectory (). GetAbsolutePath () gets the absolute path;

(4-3) Downloadmanager object Call getinstance () to get Downloadmanager object;

(4-4) Downloadmanager object calls Startdownload (), passing in six parameters:

Parameter 1: Download path;

Parameter 2: Save the Tag;

Parameter 3: File path;

Parameter 4: Whether to support the continuation of the breakpoint;

Parameter 5: whether to support renaming;

Parameters 6:null NULL parameter;

5. Database function: Can be set up and delete changes, database information in the form of module storage;

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

Add annotations before current JavaBean: @TABLE (), Incoming Name= "", as table name;

Add annotations before attributes in JavaBean: @Column (), pass in Id= "" as the field ID, pass in name= "" as the field name;

    Note : In the database corresponding to the JavaBean must have the id attribute, the binding annotation is @column (isid=true, Name= ""), otherwise it will run the Times null pointer exception;

(2) Configure database information in activity:

(2-1) Create Dbmanager.daoconfig object;

(2-2) Setdbname (), incoming database name;

(2-3) Setdbopenlistener (), passing in an object that implements the Dbmanager.dbopenlistener interface, overriding ondbopened () to open the data;

(2-4) Setdbupgradelistener (), passed in an object that implements the Dbmanager.dbupgradelistener interface, overriding Ondbupgrade () is used when updating the database;

(3) in the need to operate the database where the increase and deletion check operation:

(3-1) Increase:

Create the JavaBean object, set the data, use X.GETDB () to pass in the created Dbmanager.daoconfig object, and then call Save () to pass in the JavaBean object;

(3-2) by deleting:

1) Delete entire table: X.getdb () incoming created Dbmanager.daoconfig object, then call Delete (), incoming javabean.class;

2) Delete a single record by ID: X.getdb () passes in the created Dbmanager.daoconfig object, and then calls Deletebyid (), passing in two parameters: Javabean.class and the ID of the record to delete;

3) Delete a single record according to the condition: X.getdb () passes in the created Dbmanager.daoconfig object, and then calls Delete (), passing in two parameters: Javabean.class and Wherebuilder.b (). and (). or ( ). Exo () and other data can be passed to the condition parameters;

(3-3) Change:

1) Modify a single record by ID: X.getdb () passes in the created Dbmanager.daoconfig object, and then calls Update (), passing in more than two parameters: the JavaBean object created and the name of the field to be modified;

2) Modify the record according to the condition: x.getdb () pass in the created Dbmanager.daoconfig object, then call Update (), pass in more than three parameters: Javabean.class, wherebuilder.b () pass in the condition parameter, Save the modified key value corresponding to the KeyValue object;

(3-4) Check:

X. GETDB () passes in the created Dbmanager.daoconfig object, then calls selector (), passes in the Javabean.class parameter, returns the Selector<javabean> object Then call where () in the wherebuilder.b () condition parameter according to the object, then call FindAll () to start the lookup, and return the List<javabean> object as the lookup result;

Android Advanced _ Third party framework Xutils

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.