Detailed explanation of basic usage rules after Android xutils is updated to 3.0

To tell you the truth, xutils is a development framework that I have only recently used (and just touched). As for its functions, it has simplified a lot of development steps. It can be said to be a very good development tool, but its recent update to 3.0 has not solved the problem of loading custom ImageView.

Introduction to xutils

Xutils contains many useful Android tools.

Xutils supports large file upload, more comprehensive HTTP request protocol support (10 predicates), more flexible ORM, more event annotation support, and is not affected by confusion

Xuitls is at least compatible with Android 2.2 (API level 8)

I always like to use some of the latest things. Xutils 3.0 has just been updated. It is a relatively large reconstruction. The usage rules of Baidu are basically those before 3.0, which makes developers using 3.0 encounter many obstacles when they need to solve usage. Therefore, here is a brief introduction to the usage rules of xutils 3.0. How to import the function is not the content of this article, but the last section of this article briefly explains the import method.

1. IOC framework in xutils

The first step in using xutils is to create your own application class. The code is as follows:

At androidmanifest Add the following code to the application tag of XML:

In this way, the initialization is completed.

The code for using IOC framework is as follows:

The following points need to be explained:

First, the use of IOC must be private, otherwise it will be invalid. Here's a demonstration. If you don't believe that you can change all the member variables and methods of the annotation using the IOC framework to public, then it will be invalid, except for contentview.

Second, all IOC member variables must be used in x.view() After inject (this), if it is written in front, the program will crash.

2. Xutils loading picture function

Now we need to set two permissions, as follows:

Next, load the network image into ImageView:

x.image(). bind(image," http://pic.baike.soso.com/p/20090711/20090711101754-314944703.jpg ");

You can also set parameters:

You can also set the second parameter to the image file path, and the image will be loaded from the SD card.

3. Xutils operation database

As we all know, there are many places to operate the database in an app, just like whether to log in or not. Some places can only be operated after logging in, so it must be a global variable. Therefore, the initialization of the database must be placed in the application, and the method to obtain the database must be provided, so that the database can be obtained directly anywhere in the application, And operate the database, otherwise repeated acquisition and release can only increase the unnecessary consumption of memory.

Initialize database:

The above notes are clear. It is necessary to note that setdbdir (new file ("/ sdcard")) can store the database where you want to store it. If it is not set, the database will be stored in / data / your application / database / xxx by default DB. Here we put it under the application by default.

We first create an entity class as follows:

Through the entity class, you can directly operate the database.

We add the following code to the application to add data to the database:

The code for obtaining database data in activity is as follows:

Then we will certainly get the following results:

4. Network request for xutils

Android stipulates that UI threads cannot involve network tasks. Therefore, here is a brief introduction to xutils' asynchronous network requests and synchronous self exploration.

The format used is as follows:

What you get here is the HTML page information of CSDN mobile blog experts. Take a look at the following logs to know that the network function of xutils is still very powerful.

At the end of this article, a rough imitation of the source code of CSDN app is attached. Interested parties can download it. It uses another development framework, which I use to deal with images. It is said that xutils is an evolutionary version of afinal, but in terms of pictures, we think xutils is still a little insufficient.

http://download.csdn.net/detail/liyuanjinglyj/9379103

5.. Import xutils project to Android studio

The download address is as follows:

https://github.com/wyouflf/xUtils3/tree/master

1. Copy the downloaded project to the project directory:

Second, add to settings Gradle file:

include ':app',':xutils'

III. compile into the project

IV. put build. In the xutils folder The version in gradle and the minimum version are adjusted to be consistent with the creation project

V. add the following code to build Gradle (Project: xutilsdemo)

Where red is the added code.

Click sync now to use xutils.

Related reading:

Use xutils3 0 to download project updates

The above is a detailed explanation of the basic usage rules of Android xutils updated to 3.0 introduced by Xiaobian. I hope it will be helpful to you. If you have any questions, please leave me a message and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!

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
分享
二维码
< <上一篇
下一篇>>