xUtils 最初源于Afinal框架,进行了大量重构,使得xUtils支持大文件上传,更全面的http请求协议支持(10种谓词),拥有更加灵活的ORM,更多的事件注解支持且不受混淆影响…
xUtils最低兼容android 2.2 (api level 8)。
当前xUtils主要有四大模块:
1.DbUtils模块
2.ViewUtils模块
3.HttpUtils模块
4.BitmapUtils模块
权限所需
activity_main.xml
MainActivity.java
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Toast;
import com.lidroid.xutils.DbUtils;
import com.lidroid.xutils.db.sqlite.Selector;
import com.lidroid.xutils.db.sqlite.WhereBuilder;
import com.lidroid.xutils.exception.DbException;
import java.util.List;
public class MainActivity extends AppCompatActivity {
DbUtils db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
db = DbUtils.create(this,"mydb");
}
public void saveClick(View view)
{
User user=new User("[email protected]","xiaohong");
try {
db.save(user);
Toast.makeText(MainActivity.this,"save success",Toast.LENGTH_LONG).show();
} catch (DbException e) {
e.printStackTrace();
}
}
public void findAllClick(View view)
{
try {
List list=db.findAll(User.class);
for (User u:list)
{
System.out.println(u);
}
} catch (DbException e) {
e.printStackTrace();
}
}
public void selectByClick(View view)
{
try {
User user = db.findFirst(Selector.from(User.class).where("name","=","xiaohong"));
Toast.makeText(this,user.toString(),Toast.LENGTH_LONG).show();
} catch (DbException e) {
e.printStackTrace();
}
}
public void updateClick(View view)
{
User user=new User();
user.setId(1);
user.setEmail("*********@qq.com");
user.setName("xiaoming");
try {
db.update(user,"email","name");
Toast.makeText(this,"更新成功",Toast.LENGTH_LONG).show();
} catch (DbException e) {
e.printStackTrace();
}
}
public void deleteClick(View view)
{
try {
db.delete(User.class, WhereBuilder.b("id","=","1"));
Toast.makeText(this,"删除成功",Toast.LENGTH_LONG).show();
} catch (DbException e) {
e.printStackTrace();
}
}
}
User.java
public class User {
private int id;
private String name;
private String email;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public User(String email, String name) {
this.email = email;
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public User() {
}
@Override
public String toString() {
return "User{" +
"email='" + email + '\'' +
", id=" + id +
", name='" + name + '\'' +
'}';
}
}
// xUtils的view注解要求必须提供id,以使代码混淆不受影响。
@ViewInject(R.id.textView)
TextView textView;
//@ViewInject(vale=R.id.textView, parentId=R.id.parentView)
//TextView textView;
@ResInject(id = R.string.label, type = ResType.String)
private String label;
// 取消了之前使用方法名绑定事件的方式,使用id绑定不受混淆影响
// 支持绑定多个id @OnClick({R.id.id1, R.id.id2, R.id.id3})
// or @OnClick(value={R.id.id1, R.id.id2, R.id.id3}, parentId={R.id.pid1, R.id.pid2, R.id.pid3})
// 更多事件支持参见ViewCommonEventListener类和包com.lidroid.xutils.view.annotation.event。
@OnClick(R.id.test_button)
public void testButtonClick(View v) { // 方法签名必须和接口中的要求一致
...
}
...
//在Activity中注入:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ViewUtils.inject(this); //注入view和事件
...
textView.setText("some text...");
...
}
//在Fragment中注入:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.bitmap_fragment, container, false); // 加载fragment布局
ViewUtils.inject(this, view); //注入view和事件
...
}
//在PreferenceFragment中注入:
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
ViewUtils.inject(this, getPreferenceScreen()); //注入view和事件
...
}
// 其他重载
// inject(View view);
// inject(Activity activity)
// inject(PreferenceActivity preferenceActivity)
// inject(Object handler, View view)
// inject(Object handler, Activity activity)
// inject(Object handler, PreferenceGroup preferenceGroup)
// inject(Object handler, PreferenceActivity preferenceActivity)
JsonObject.java
import java.util.ArrayList;
public class JsonObject {
private String login;
private ArrayList user=new ArrayList();
public String getLogin() {
return login;
}
public void setLogin(String login) {
this.login = login;
}
public ArrayList getUser() {
return user;
}
public void setUser(ArrayList user) {
this.user = user;
}
@Override
public String toString() {
return "JsonObject{" +
"login='" + login + '\'' +
", user=" + user +
'}';
}
}
User.java
public class User {
private int id;
private String username;
private String password;
private String role;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
public User(int id,String username, String password, String role) {
this.id=id;
this.username = username;
this.password = password;
this.role = role;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
", role='" + role + '\'' +
'}';
}
public User()
{
}
}
MainActivity2.java
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.google.gson.Gson;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import java.util.ArrayList;
public class MainActivity2 extends Activity {
private EditText editText_username;
private EditText editText_password;
private Button button_login;
private TextView textView_info;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
editText_username= (EditText) findViewById(R.id.editText_username);
editText_password= (EditText) findViewById(R.id.editText_password);
button_login= (Button) findViewById(R.id.button_login);
textView_info=(TextView)findViewById(R.id.info);
button_login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String username=editText_username.getText().toString();
String password=editText_password.getText().toString();
HttpUtils httpUtils=new HttpUtils();
String url="http://192.168.1.101:8080/user/LoginServlet";
RequestParams params=new RequestParams();
params.addQueryStringParameter("username",username);
params.addBodyParameter("password",password);
httpUtils.send(HttpRequest.HttpMethod.POST, url, params, new RequestCallBack() {
@Override
public void onStart() {
super.onStart();
textView_info.setText("conn...");
}
@Override
public void onLoading(long total, long current, boolean isUploading) {
super.onLoading(total, current, isUploading);
if (isUploading) {
textView_info.setText("upload: " + current + "/" + total);
} else {
textView_info.setText("reply: " + current + "/" + total);
}
}
@Override
public void onSuccess(ResponseInfo responseInfo) {
String result=responseInfo.result;
System.out.println(result);
Gson gson = new Gson();
JsonObject object = gson.fromJson(result, JsonObject.class);
ArrayList list=object.getUser();
int count=list.size();
String username="";
for (int i=0;i
activity_main2.xml
product.java
import cn.bmob.v3.BmobObject;
import cn.bmob.v3.datatype.BmobFile;
public class product extends BmobObject{
private String name;
private BmobFile pic;
private double price;
private double discount;
public product()
{
}
public product(String name, BmobFile pic, double price, double discount) {
this.name = name;
this.pic = pic;
this.price = price;
this.discount = discount;
}
public double getDiscount() {
return discount;
}
public void setDiscount(double discount) {
this.discount = discount;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public BmobFile getPic() {
return pic;
}
public void setPic(BmobFile pic) {
this.pic = pic;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
ProductListAdapter.java
import android.content.Context;
import android.os.Environment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.lidroid.xutils.BitmapUtils;
import java.util.List;
public class ProductListAdapter extends BaseAdapter {
private Context context;
private List products;
public ProductListAdapter(Context context, List products) {
this.context = context;
this.products = products;
}
public List getProducts() {
return products;
}
public void setProducts(List products) {
this.products = products;
}
@Override
public int getCount() {
return products.size();
}
@Override
public Object getItem(int position) {
return products.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder vh;
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.product_list_item, null);
vh = new ViewHolder();
vh.textView_name = (TextView) convertView.findViewById(R.id.textView_name);
vh.textView_price = (TextView) convertView.findViewById(R.id.textView_price);
vh.textView_discount = (TextView) convertView.findViewById(R.id.textView_discount);
vh.imageView_product_image = (ImageView) convertView.findViewById(R.id.imageView_product_image);
convertView.setTag(vh);
}
vh = (ViewHolder) convertView.getTag();
product p = products.get(position);
vh.textView_name.setText(p.getName());
vh.textView_price.setText("¥" + Math.round((p.getPrice() * (p.getDiscount() / 10) * 100.00)) / 100.00);
vh.textView_discount.setText(String.valueOf(p.getDiscount()) + "折");
//用于缓存图片文件 Lru算法
//用于缓存图片文件 Lru算法
String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/x-utils";
BitmapUtils bitmapUtils = new BitmapUtils(context, path);
bitmapUtils.configDefaultBitmapMaxSize(100, 100);
bitmapUtils.display(vh.imageView_product_image, p.getPic().getFileUrl(context));
return convertView;
}
static class ViewHolder {
TextView textView_name;
TextView textView_price;
TextView textView_discount;
ImageView imageView_product_image;
}
}
ProductListActivity.java
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.listener.FindListener;
public class ProductListActivity extends AppCompatActivity {
private ListView productListView;
private ProductListAdapter pla;
private List products=new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_product_list);
// 初始化 Bmob SDK
// 使用时请将第二个参数Application ID替换成你在Bmob服务器端创建的Application ID
Bmob.initialize(this, "Application ID");
productListView=(ListView)findViewById(R.id.product_list_view);
pla=new ProductListAdapter(ProductListActivity.this,products);
loadData();
}
private void loadData()
{
final View loadView=getLayoutInflater().inflate(R.layout.load_data,null);
productListView.addHeaderView(loadView);
productListView.setAdapter(pla);
BmobQuery query=new BmobQuery<>();
query.findObjects(ProductListActivity.this, new FindListener() {
@Override
public void onSuccess(List list) {
System.out.println(list.size());
products=list;
productListView.removeHeaderView(loadView);
pla.setProducts(products);
pla.notifyDataSetChanged();
}
@Override
public void onError(int i, String s) {
}
});
}
}
button_bg.xml
-
load_data.xml
product_list_item.xml
activity_product_list.xml