Android advanced _ third-party framework Xutils and android framework xutils

Source: Internet
Author: User

Android advanced _ third-party framework Xutils and android framework xutils

The xutils function consists of four parts: (1) Layout View Association; (2) image download and cache; (3) network requests; (4) database;

1. Use xutils for view injection:

(1) Add @ ViewInject () to the control resource Id above the control declaration;

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

(3) set the value click event:

(3-1) custom 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 input the resource Id of the control;

Note: If you set multiple controls to be in the same Click Event, input an int array in @ Event (), and set the resource IDs of different controls in the array;

(3-3) input the second parameter in @ Event () as the Event type. The default type is OnClickListener. class, which can be changed to type = AdapterView. OnItemClickListener. class;

3. Download network images:

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

(2) declare and instantiate the ImageView control;

(3) initialize the xutils network request in Activity or Application's onCreate (): Call x. Ext. init (getApplication ());

(3-1) Use onCreate () in the Custom class that inherits the Application:

(3-2) onTerminate (), which is called when the program exits;

(3-3) onLowMemeory (), this method is called when the program runs out of memory;

(4) set image parameters:

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

(4-2) setConfig (Bitmap. Config. RGB_565) sets image parameters;

(4-3) setUseMemCache (true) sets whether the image is cached. Note: The 3G cache function is used here;

(4-4) setRadius (10) sets the image rounded corner. Note: use it with caution. It is used to process the image rather than directly operate the ImageView;

(4-5) setLoadingDrawableId () sets the image resource Id during loading;

(4-6) setFailureDrawableId () sets the resource Id displayed when the image download fails;

(4-7) setIgnoreGif () sets whether to ignore GIF images; false indicates dynamic display;

(4-8) The ImageOptions. Builder object calls build (), returns the ImageOptions object, and starts the created image parameter object;

(5) Start image download and call x. image (). bind () to input two or three parameters:

Parameter 1: The ImageView control to be displayed;

Parameter 2: Image download path;

Parameter 3: (optional) Image Parameters of the ImageOptions object;

ImageView image loading process: At first, images will be loaded from the memory. If an image is found, the image will be directly returned. If not, the image will be retrieved from the hard disk cache, if the hard disk cache does not exist, images will be loaded from the network; If images are loaded from the network, images will be saved in the memory and SDcard;

4. network requests

(1) get download request:

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

(1-2) Set Request Parameters: Create a RequestParams object and input the network download path;

(1-3) start downloading data: x. http (). get (). Input two parameters and 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 the download is successful;

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

Method 3: onCancelled (): This method is called when downloading is canceled;

Method 4: onFinished (): This method is called when the download ends;

(1-5) in onDestroy (), it is determined that the Cancelable object is not empty and can be canceled when the network download task is canceled: The Cancelable object can be called cancel;

(2) post upload request:

(2-1) Add network access permissions, read and write permissions to the inventory file;

(2-2) Set Request Parameters: Create a RequestParams object and pass in the network upload path;

(2-3) The RequestParams object calls addBodyParameter () to pass in data in the form of a key-value pair;

(2-4) send the request: x. http (). post (). Input two parameters and return the Callback. Cancelable object:

Parameter 1: RequestParams object;

Parameter 2: Callback listener implements the Callback. CommonCallback <String> interface. In this class, rewrite four methods:

Method 1: onSuccess (): This method is called back when the upload is successful;

Method 2: onError (): callback when the upload fails;

Method 3: onCancelled (): callback when the upload is canceled;

Method 4: onFinished (): This method is called back when the upload ends;

(3) upload File: In the RequestParams object, create the upload File Path and put it in the form of a key-value pair into the RequestParams object to call addBodyParameter (); other parts are the same as the uploaded data;

(4) download an object

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

(4-2) create a file download path: the Environment object calls getExternalStorageDirectory (). getAbsolutePath () to obtain the absolute path;

(4-3) Call getInstance () to obtain the DownloadManager object;

(4-4) The DownloadManager object calls startDownload () and passes in six parameters:

Parameter 1: Download path;

Parameter 2: Save the tag;

Parameter 3: file path;

Parameter 4: whether resumable data transfer is supported;

Parameter 5: whether rename is supported;

Parameter 6: null Parameter;

5. Database Function: You can set addition, deletion, modification, and query. database information is stored as a Module;

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

Add the annotation @ TABLE () before the current JavaBean and input name = "as the TABLE name;

Add the annotation @ Column () before the attribute in the JavaBean, and input id = "" as the field Id, and input name = "" as the field name;

Note: The id attribute must be included in the JavaBean corresponding to the database, and the annotation is bound to @ Column (isId = true, name = ""). Otherwise, a null pointer exception will be reported during running;

(2) configure the database information in the Activity:

(2-1) create a DbManager. DaoConfig object;

(2-2) setDbName (), input the database name;

(2-3) setDbOpenListener (), pass in the object implementing the DbManager. DbOpenListener interface, and rewrite onDbOpened () to open the data;

(2-4) setDbUpgradeListener (), pass in the object implementing the DbManager. DbUpgradeListener interface, and rewrite onDbUpgrade () to use it when updating the database;

(3) add, delete, modify, and query database operations:

(3-1) increment:

Create a JavaBean object and set data. Use x. getDb () to input the created DbManager. DaoConfig object, call save (), and pass in the JavaBean object;

(3-2) Delete:

1) delete the entire table: x. getDb () is used to input the created DbManager. DaoConfig object, call delete (), and input JavaBean. class;

2) delete a single record by Id: x. getDb () is passed in to the created DbManager. daoConfig object, and then call deleteById () to input two parameters: JavaBean. class and the Id of the record to be deleted;

3) delete a single record according to the condition: x. getDb () is passed in to the created DbManager. daoConfig object, and then call delete () to pass in two parameters: JavaBean. class and WhereBuilder. B (). and (). or (). conditional parameters that can be input for exo () and other data;

(3-3) change:

1) modify a single record by Id: x. getDb () is passed in to the created DbManager. daoConfig object, then call update (), and input two or more parameters: the created JavaBean object and the field name to be modified;

2) modify the record according to the condition: x. getDb () is passed in to the created DbManager. daoConfig object, and then call update () to input more than three parameters: JavaBean. class, WhereBuilder. B () input the condition parameter to save the modified key value corresponding to the KeyValue object;

(3-4) query:

X. getDb () is passed in to the created DbManager. daoConfig object, and then call selector () to input JavaBean. class parameter, returns the Selector <JavaBean> object, and then calls where () based on this object to pass in whereBuilder. B () condition parameter, and then call findAll () to start searching. The List <JavaBean> object is returned as the search result;

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.