MainActvity
package bw.com.bw_day01_xutils;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;
import bw.com.bw_day01_xutils.demo02.GetDataActivity;
import bw.com.bw_day01_xutils.demo03.DownLoadApkActivity;
import bw.com.bw_day01_xutils.demo04.DbActivity;
//TODO 2, 利用注解 加载页面
@ContentView(value = R.layout.activity_main)
public class MainActivity extends AppCompatActivity {
//TODO 3, 控件的初始化
@ViewInject(value = R.id.tv_id)
private TextView mTv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);//加载页面布局
//TODO ViewUtils 的使用
//TODO 1, 需要引入注解
x.view().inject(this);
}
//TODO 4, 事件的注解
/**注意:
* 1, 需要@Event 实现事件的注解
* 2, value , 必须是一个int[] , 可以设置多个控件的ID
* 3, type , 类型-- 要处理的是哪个事件 -- 事件类.class
* 4, 方法的修饰符必须是私有的
* 5, 方法的名称, 必须要和type 指定的事件监听器中, 回调的方法名一致
*/
@Event(value = {R.id.tv_id,R.id.but_01,R.id.but_02,R.id.but_03,R.id.but_04},type = View.OnClickListener.class)
private void onClick(View view)
{
switch (view.getId())
{
case R.id.tv_id:
mTv.setText("xUtils 的注解功能就写完了........");
break;
case R.id.but_01:
Toast.makeText(this, "你点击了按钮", Toast.LENGTH_SHORT).show();
break;
case R.id.but_02:
startActivity(new Intent(this, GetDataActivity.class));
break;
case R.id.but_03:
startActivity(new Intent(this, DownLoadApkActivity.class));
break;
case R.id.but_04:
startActivity(new Intent(this, DbActivity.class));
break;
}
}
}
MyApp
package bw.com.bw_day01_xutils;
import android.app.Application;
import org.xutils.x;
/**
* 3, 定义Application 的子类, 初始化xUtils
*/
public class MyApp extends Application{
@Override
public void onCreate() {
super.onCreate();
//初始化
x.Ext.init(this);
x.Ext.setDebug(true);//设置显示日志
}
}
Demo02
GeteDataActivity
package bw.com.bw_day01_xutils.demo02;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ListView;
import com.google.gson.Gson;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;
import java.util.List;
import bw.com.bw_day01_xutils.R;
/**
* 需求: 通过xUtils 获取网络数据, 解析数据, 将数据显示在ListView 中
*
* 1, 导入依赖和jar包
* 2, 添加权限
* 3, 初始化xUtils -- Application 的子类中 -- MyApp -- 在清单文件中引入App
*/
//TODO 加载页面
@ContentView(value = R.layout.activity_get_data)
public class GetDataActivity extends AppCompatActivity {
//TODO 初始化控件
@ViewInject(value = R.id.lv_id)
private ListView mLv;
//数据源 -- 网络获取 -- 实体类
private List<VMoive.DataBean> data;
//适配器
private MyAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//TODO 引入注解
x.view().inject(this);
//TODO 重点: xUtils 获取网络数据
//声明请求参数( 下载地址);
RequestParams params = new RequestParams("http://app.vmoiver.com/apiv3/post/getPostInCate?cateid=0&p=1");
//1参: 请求参数 ; 2参: 回调接口
x.http().get(params, new Callback.CommonCallback<String>() {
@Override
public void onSuccess(String s) {
//TODO 获取数据成功的回调方法 s -- 返回的json字符串
//解析数据
VMoive vMoive = new Gson().fromJson(s,VMoive.class);
//得到数据源
data = vMoive.getData();
//初始化适配器
adapter = new MyAdapter(GetDataActivity.this,data);
mLv.setAdapter(adapter);
}
@Override
public void onError(Throwable throwable, boolean b) {
//TODO 获取数据失败的回调方法
}
@Override
public void onCancelled(CancelledException e) {
//TODO 取消的回调方法
}
@Override
public void onFinished() {
//TODO 获取数据完成的方法
}
});
}
}
MyAdapter
package bw.com.bw_day01_xutils.demo02;
import android.content.Context;
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 org.xutils.image.ImageOptions;
import org.xutils.x;
import java.util.List;
import bw.com.bw_day01_xutils.R;
/**
* Created by Administrator on 2018/1/23.
*/
public class MyAdapter extends BaseAdapter {
private Context context;
private List<VMoive.DataBean> data;
public MyAdapter(Context context,List<VMoive.DataBean> data)
{
this.context = context;
this.data = data;
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return data.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if(convertView==null)
{
convertView = LayoutInflater.from(context).inflate(R.layout.item_lv,parent,false);
viewHolder = new ViewHolder();
viewHolder.tv = (TextView) convertView.findViewById(R.id.tv_id);
viewHolder.iv = (ImageView) convertView.findViewById(R.id.iv_id);
convertView.setTag(viewHolder);
}else
{
viewHolder = (ViewHolder) convertView.getTag();
}
//赋值 -- 设置标题
viewHolder.tv.setText(data.get(position).getTitle());
// 加载图片
//图片的选项
ImageOptions options = new ImageOptions.Builder()
.setFailureDrawableId(R.mipmap.ic_launcher)//加载失败显示的图片
.setLoadingDrawableId(R.mipmap.ic_launcher)//正在加载时显示的图片
.setUseMemCache(true)//设置缓存
.setCircular(true)//设置圆形图片
.setIgnoreGif(false)//设置是否忽略gif动画
.build();
x.image().bind(viewHolder.iv,data.get(position).getImage(),options);
return convertView;
}
class ViewHolder
{
ImageView iv;
TextView tv;
}
}
VMoive
package bw.com.bw_day01_xutils.demo02;
import java.util.List;
/**
* Created by Administrator on 2018/1/23.
*/
public class VMoive {
/**
* status : 0
* msg : OK
* data : [{"postid":"53381","title":"被这只可爱的蜘蛛萌化了","wx_small_app_title":"被这只可爱的蜘蛛萌化了","pid":"1","app_fu_title":"","is_xpc":"0","is_promote":"0","is_xpc_zp":"0","is_xpc_cp":"0","is_xpc_fx":"0","is_album":"0","tags":"","recent_hot":"0","discussion":"0","image":"https://cs.vmovier.com/Uploads/cover/2018-01-22/5a65b4a4483c6_cut.jpeg","rating":"7.8","duration":"88","publish_time":"1516637160","like_num":"203","share_num":"237","post_type":"1","cates":[{"cateid":"16","catename":"动画"}],"request_url":"http://app.vmoiver.com/53381?qingapp=app_new","ispromote":"0","isalbum":"0","cate":[{"cateid":"16","catename":"动画"}]},{"postid":"53393","title":"杨幂、张彬彬OPPO甜蜜广告「两个小星球」","wx_small_app_title":"杨幂、张彬彬OPPO甜蜜广告「两个小星球」","pid":"1","app_fu_title":"","is_xpc":"1","is_promote":"0","is_xpc_zp":"1","is_xpc_cp":"0","is_xpc_fx":"0","is_album":"0","tags":"","recent_hot":"0","discussion":"0","image":"https://cs.vmovier.com/Uploads/cover/2018-01-22/5a65c0c2bdb58_cut.jpeg","rating":"6.7","duration":"249","publish_time":"1516637100","like_num":"116","share_num":"119","post_type":"1","cates":[{"cateid":"13","catename":"广告"}],"request_url":"http://app.vmoiver.com/53393?qingapp=app_new","ispromote":"0","isalbum":"0","cate":[{"cateid":"13","catename":"广告"}]},{"postid":"53371","title":"少儿不宜:最特别的成人礼物","wx_small_app_title":"少儿不宜:最特别的成人礼物","pid":"1","app_fu_title":"","is_xpc":"0","is_promote":"0","is_xpc_zp":"0","is_xpc_cp":"0","is_xpc_fx":"0","is_album":"0","tags":"","recent_hot":"0","discussion":"0","image":"https://cs.vmovier.com/Uploads/cover/2018-01-19/5a61986e8ceb2_cut.jpeg","rating":"7.6","duration":"746","publish_time":"1516637040","like_num":"119","share_num":"80","post_type":"1","cates":[{"cateid":"17","catename":"剧情"}],"request_url":"http://app.vmoiver.com/53371?qingapp=app_new","ispromote":"0","isalbum":"0","cate":[{"cateid":"17","catename":"剧情"}]},{"postid":"53357","title":"他所记录的香港电影的黄金时代","wx_small_app_title":"他所记录的香港电影的黄金时代","pid":"1","app_fu_title":"","is_xpc":"1","is_promote":"0","is_xpc_zp":"1","is_xpc_cp":"0","is_xpc_fx":"0","is_album":"0","tags":"","recent_hot":"0","discussion":"0","image":"https://cs.vmovier.com/Uploads/cover/2018-01-17/5a5f3f8bb7754_cut.jpeg","rating":"7.5","duration":"220","publish_time":"1516636980","like_num":"109","share_num":"42","post_type":"1","cates":[{"cateid":"24","catename":"纪录"}],"request_url":"http://app.vmoiver.com/53357?qingapp=app_new","ispromote":"0","isalbum":"0","cate":[{"cateid":"24","catename":"纪录"}]},{"postid":"53378","title":"\u201c锤哥\u201d周六夜现场,耍贱卖萌停不下来","wx_small_app_title":"\u201c锤哥\u201d周六夜现场,耍贱卖萌停不下来","pid":"1","app_fu_title":"","is_xpc":"0","is_promote":"0","is_xpc_zp":"0","is_xpc_cp":"0","is_xpc_fx":"0","is_album":"0","tags":"","recent_hot":"0","discussion":"0","image":"https://cs.vmovier.com/Uploads/cover/2018-01-19/5a61b78fe995d_cut.jpeg","rating":"7.0","duration":"95","publish_time":"1516636920","like_num":"81","share_num":"30","post_type":"1","cates":[{"cateid":"8","catename":"搞笑"}],"request_url":"http://app.vmoiver.com/53378?qingapp=app_new","ispromote":"0","isalbum":"0","cate":[{"cateid":"8","catename":"搞笑"}]},{"postid":"53380","title":"「爱在记忆消逝前」定档预告","wx_small_app_title":"「爱在记忆消逝前」定档预告","pid":"1","app_fu_title":"","is_xpc":"0","is_promote":"1","is_xpc_zp":"0","is_xpc_cp":"0","is_xpc_fx":"0","is_album":"0","tags":"推广","recent_hot":"0","discussion":"0","image":"https://cs.vmovier.com/Uploads/cover/2018-01-22/5a6555343204d_cut.jpeg","rating":"7.5","duration":"126","publish_time":"1516593431","like_num":"225","share_num":"341","post_type":"1","cates":[{"cateid":"43","catename":"预告"}],"request_url":"http://app.vmoiver.com/53380?qingapp=app_new","ispromote":"1","isalbum":"0","cate":[{"cateid":"43","catename":"预告"}]},{"postid":"53373","title":"这世界上的每个人都很重要","wx_small_app_title":"这世界上的每个人都很重要","pid":"1","app_fu_title":"","is_xpc":"0","is_promote":"0","is_xpc_zp":"0","is_xpc_cp":"0","is_xpc_fx":"0","is_album":"0","tags":"","recent_hot":"0","discussion":"0","image":"https://cs.vmovier.com/Uploads/cover/2018-01-19/5a619f20bad9b_cut.jpeg","rating":"7.0","duration":"168","publish_time":"1516550760","like_num":"618","share_num":"602","post_type":"1","cates":[{"cateid":"13","catename":"广告"}],"request_url":"http://app.vmoiver.com/53373?qingapp=app_new","ispromote":"0","isalbum":"0","cate":[{"cateid":"13","catename":"广告"}]},{"postid":"53376","title":"创意定格城市掠影:爱惧柏林","wx_small_app_title":"创意定格城市掠影:爱惧柏林","pid":"1","app_fu_title":"","is_xpc":"0","is_promote":"0","is_xpc_zp":"0","is_xpc_cp":"0","is_xpc_fx":"0","is_album":"0","tags":"","recent_hot":"0","discussion":"0","image":"https://cs.vmovier.com/Uploads/cover/2018-01-19/5a61a20460b42_cut.jpeg","rating":"7.1","duration":"127","publish_time":"1516550700","like_num":"302","share_num":"162","post_type":"1","cates":[{"cateid":"6","catename":"创意"}],"request_url":"http://app.vmoiver.com/53376?qingapp=app_new","ispromote":"0","isalbum":"0","cate":[{"cateid":"6","catename":"创意"}]},{"postid":"53363","title":"触目惊心短片:肢解死刑犯","wx_small_app_title":"触目惊心短片:肢解死刑犯","pid":"1","app_fu_title":"","is_xpc":"0","is_promote":"0","is_xpc_zp":"0","is_xpc_cp":"0","is_xpc_fx":"0","is_album":"0","tags":"","recent_hot":"0","discussion":"0","image":"https://cs.vmovier.com/Uploads/cover/2018-01-18/5a6063646ff97_cut.jpeg","rating":"7.9","duration":"774","publish_time":"1516550640","like_num":"432","share_num":"381","post_type":"1","cates":[{"cateid":"17","catename":"剧情"}],"request_url":"http://app.vmoiver.com/53363?qingapp=app_new","ispromote":"0","isalbum":"0","cate":[{"cateid":"17","catename":"剧情"}]},{"postid":"53362","title":"回忆与现实的纠葛:影子爱人","wx_small_app_title":"回忆与现实的纠葛:影子爱人","pid":"1","app_fu_title":"","is_xpc":"0","is_promote":"0","is_xpc_zp":"0","is_xpc_cp":"0","is_xpc_fx":"0","is_album":"0","tags":"","recent_hot":"0","discussion":"0","image":"https://cs.vmovier.com/Uploads/cover/2018-01-18/5a6054ff48747_cut.jpeg","rating":"7.5","duration":"601","publish_time":"1516550580","like_num":"230","share_num":"182","post_type":"1","cates":[{"cateid":"16","catename":"动画"}],"request_url":"http://app.vmoiver.com/53362?qingapp=app_new","ispromote":"0","isalbum":"0","cate":[{"cateid":"16","catename":"动画"}]}]
*/
private String status;
private String msg;
private List<DataBean> data;
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public List<DataBean> getData() {
return data;
}
public void setData(List<DataBean> data) {
this.data = data;
}
public static class DataBean {
/**
* postid : 53381
* title : 被这只可爱的蜘蛛萌化了
* wx_small_app_title : 被这只可爱的蜘蛛萌化了
* pid : 1
* app_fu_title :
* is_xpc : 0
* is_promote : 0
* is_xpc_zp : 0
* is_xpc_cp : 0
* is_xpc_fx : 0
* is_album : 0
* tags :
* recent_hot : 0
* discussion : 0
* image : https://cs.vmovier.com/Uploads/cover/2018-01-22/5a65b4a4483c6_cut.jpeg
* rating : 7.8
* duration : 88
* publish_time : 1516637160
* like_num : 203
* share_num : 237
* post_type : 1
* cates : [{"cateid":"16","catename":"动画"}]
* request_url : http://app.vmoiver.com/53381?qingapp=app_new
* ispromote : 0
* isalbum : 0
* cate : [{"cateid":"16","catename":"动画"}]
*/
private String postid;
private String title;
private String wx_small_app_title;
private String pid;
private String app_fu_title;
private String is_xpc;
private String is_promote;
private String is_xpc_zp;
private String is_xpc_cp;
private String is_xpc_fx;
private String is_album;
private String tags;
private String recent_hot;
private String discussion;
private String image;
private String rating;
private String duration;
private String publish_time;
private String like_num;
private String share_num;
private String post_type;
private String request_url;
private String ispromote;
private String isalbum;
private List<CatesBean> cates;
private List<CateBean> cate;
public String getPostid() {
return postid;
}
public void setPostid(String postid) {
this.postid = postid;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getWx_small_app_title() {
return wx_small_app_title;
}
public void setWx_small_app_title(String wx_small_app_title) {
this.wx_small_app_title = wx_small_app_title;
}
public String getPid() {
return pid;
}
public void setPid(String pid) {
this.pid = pid;
}
public String getApp_fu_title() {
return app_fu_title;
}
public void setApp_fu_title(String app_fu_title) {
this.app_fu_title = app_fu_title;
}
public String getIs_xpc() {
return is_xpc;
}
public void setIs_xpc(String is_xpc) {
this.is_xpc = is_xpc;
}
public String getIs_promote() {
return is_promote;
}
public void setIs_promote(String is_promote) {
this.is_promote = is_promote;
}
public String getIs_xpc_zp() {
return is_xpc_zp;
}
public void setIs_xpc_zp(String is_xpc_zp) {
this.is_xpc_zp = is_xpc_zp;
}
public String getIs_xpc_cp() {
return is_xpc_cp;
}
public void setIs_xpc_cp(String is_xpc_cp) {
this.is_xpc_cp = is_xpc_cp;
}
public String getIs_xpc_fx() {
return is_xpc_fx;
}
public void setIs_xpc_fx(String is_xpc_fx) {
this.is_xpc_fx = is_xpc_fx;
}
public String getIs_album() {
return is_album;
}
public void setIs_album(String is_album) {
this.is_album = is_album;
}
public String getTags() {
return tags;
}
public void setTags(String tags) {
this.tags = tags;
}
public String getRecent_hot() {
return recent_hot;
}
public void setRecent_hot(String recent_hot) {
this.recent_hot = recent_hot;
}
public String getDiscussion() {
return discussion;
}
public void setDiscussion(String discussion) {
this.discussion = discussion;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public String getRating() {
return rating;
}
public void setRating(String rating) {
this.rating = rating;
}
public String getDuration() {
return duration;
}
public void setDuration(String duration) {
this.duration = duration;
}
public String getPublish_time() {
return publish_time;
}
public void setPublish_time(String publish_time) {
this.publish_time = publish_time;
}
public String getLike_num() {
return like_num;
}
public void setLike_num(String like_num) {
this.like_num = like_num;
}
public String getShare_num() {
return share_num;
}
public void setShare_num(String share_num) {
this.share_num = share_num;
}
public String getPost_type() {
return post_type;
}
public void setPost_type(String post_type) {
this.post_type = post_type;
}
public String getRequest_url() {
return request_url;
}
public void setRequest_url(String request_url) {
this.request_url = request_url;
}
public String getIspromote() {
return ispromote;
}
public void setIspromote(String ispromote) {
this.ispromote = ispromote;
}
public String getIsalbum() {
return isalbum;
}
public void setIsalbum(String isalbum) {
this.isalbum = isalbum;
}
public List<CatesBean> getCates() {
return cates;
}
public void setCates(List<CatesBean> cates) {
this.cates = cates;
}
public List<CateBean> getCate() {
return cate;
}
public void setCate(List<CateBean> cate) {
this.cate = cate;
}
public static class CatesBean {
/**
* cateid : 16
* catename : 动画
*/
private String cateid;
private String catename;
public String getCateid() {
return cateid;
}
public void setCateid(String cateid) {
this.cateid = cateid;
}
public String getCatename() {
return catename;
}
public void setCatename(String catename) {
this.catename = catename;
}
}
public static class CateBean {
/**
* cateid : 16
* catename : 动画
*/
private String cateid;
private String catename;
public String getCateid() {
return cateid;
}
public void setCateid(String cateid) {
this.cateid = cateid;
}
public String getCatename() {
return catename;
}
public void setCatename(String catename) {
this.catename = catename;
}
}
}
}
Demo03
DownLoadApkActivity
package bw.com.bw_day01_xutils.demo03;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ProgressBar;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;
import java.io.File;
import bw.com.bw_day01_xutils.R;
@ContentView(value = R.layout.activity_down_load_apk)
public class DownLoadApkActivity extends AppCompatActivity {
//初始化控件
@ViewInject(value = R.id.pb_id)
private ProgressBar mProgressBar;
//下载的地址:
private String apkUrl = "http://218.244.149.129:9010/download.php?apkid=12";
//sd卡中存储的名称
private String apkName = "hello.apk";
//TODO 2, 断点续传中, 设置可以取消的任务
private Callback.Cancelable cancelable;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//引入注解
x.view().inject(this);
}
//点击按钮, 下载apk, 存入sd中 -- 路径, 最后实现自动安装的功能
@Event(value = {R.id.down_load_id,R.id.stop_load_id})
private void onClick(View view)
{
switch (view.getId())
{
case R.id.down_load_id: // 点击下载
//请求参数
RequestParams params = new RequestParams(apkUrl);
//TODO 1 , 设置支持断点续传
params.setAutoResume(true);//设置支持断点续传
//存入sd中 -- 指定路径 getExternalCacheDir() 混存目录
String filePath = getExternalCacheDir() + File.separator + apkName;
//指定路径, 到网络请求参数中
params.setSaveFilePath(filePath);
//TODO 3, 断点续传中, 获取可以取消的任务
cancelable = x.http().get(params, new Callback.ProgressCallback<File>() {
@Override
public void onLoading(long total, long current, boolean isDownLoading) {
// todo 正在加载的方法 -- 实时更新进度
int progress = (int)(current *100/total);//得到当前加载的进度
mProgressBar.setProgress(progress);
}
@Override
public void onSuccess(File file) {
// todo 加载成功 -- 最后的操作 -- 一定是下载成功了, 所有只需要安装即可
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file),"application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
DownLoadApkActivity.this.startActivity(intent);
}
@Override
public void onError(Throwable throwable, boolean b) {
// todo 加载失败
}
@Override
public void onCancelled(CancelledException e) {
// todo 取消方法
}
@Override
public void onFinished() {
// todo 完成的方法
}
@Override
public void onWaiting() {
// todo 等待的方法
}
@Override
public void onStarted() {
// todo 开始加载的方法
}
});
break;
case R.id.stop_load_id: //点击暂停
//TODO 4, 暂停下载
cancelable.cancel();
break;
}
}
}
Demo04
DbActivity
package bw.com.bw_day01_xutils.demo04;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import org.xutils.DbManager;
import org.xutils.ex.DbException;
import org.xutils.view.annotation.ContentView;
import org.xutils.x;
import java.util.List;
import bw.com.bw_day01_xutils.R;
@ContentView(value = R.layout.activity_db)
public class DbActivity extends AppCompatActivity {
private DbManager dbManager;//数据库的管理器对象
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//引入注解
x.view().inject(this);
//1, 创建数据库 -- 实例化数据库的配置信息
DbManager.DaoConfig daoConfig = new DbManager.DaoConfig();
daoConfig.setDbName("bw_1511C.db");//数据库的名称
daoConfig.setDbVersion(1);//设置版本号
daoConfig.setDbUpgradeListener(new DbManager.DbUpgradeListener() {
@Override
public void onUpgrade(DbManager dbManager, int i, int i1) {
}
});//数据库版本更新的回调方法
dbManager = x.getDb(daoConfig);
//2, 表 -- 实体类 (改良) --TODO 切记: 必须要包含一个无参的构造方法
//3, 数据--内容 -- 添加数据
try {
dbManager.save(new User("张三",20));
dbManager.save(new User("李四",25));
dbManager.save(new User("王五",30));
} catch (DbException e) {
e.printStackTrace();
}
//5, 修改数据
//根据id 得到要修改的类
try {
User user = dbManager.findById(User.class,1);
user.setName("宋小宝");
user.setAge(40);
dbManager.update(user,"name","age");
} catch (DbException e) {
e.printStackTrace();
}
//6,删除数据
try {
//根据id 得到要删除的类
User user = dbManager.findById(User.class,2);
dbManager.delete(user);
} catch (DbException e) {
e.printStackTrace();
}
//4, 查看数据
try {
//查询所有数据
List<User> data = dbManager.findAll(User.class);
//查询指定id的数据 dbManager.findById(User.class,2);
for(User user:data)
{
Log.e("TAG","user -- " + user);
}
} catch (DbException e) {
e.printStackTrace();
}
}
}
User
package bw.com.bw_day01_xutils.demo04;
import org.xutils.db.annotation.Column;
import org.xutils.db.annotation.Table;
/**
* Created by Administrator on 2018/1/23.
*/
//指定表
@Table(name = "user")
public class User {
//指定字段
//name 字段名称,isId = true 是否为主键 ,autoGen = true 是否自动增长
@Column(name = "_id",isId = true,autoGen = true)
private int id;
@Column(name = "name")
private String name;
@Column(name = "age")
private int age;
public User() {
}
public User(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
'}';
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
权限
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="bw.com.bw_day01_xutils">
<!-- 2, 添加权限 -->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application
android:name=".MyApp"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".demo02.GetDataActivity" />
<activity android:name=".demo03.DownLoadApkActivity" />
<activity android:name=".demo04.DbActivity"></activity>
</application>
</manifest>
布局
activity_db
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="bw.com.bw_day01_xutils.demo04.DbActivity">
</android.support.constraint.ConstraintLayout>
activity_down_load_apk
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="bw.com.bw_day01_xutils.demo03.DownLoadApkActivity">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="下载"
android:id="@+id/down_load_id"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="暂停"
android:id="@+id/stop_load_id"/>
<ProgressBar
android:id="@+id/pb_id"
android:layout_width="match_parent"
android:layout_height="10dp"
android:layout_marginTop="20dp"
style="?android:attr/progressBarStyleHorizontal"
/>
</LinearLayout>
activity_get_data
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="bw.com.bw_day01_xutils.demo02.GetDataActivity">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/lv_id"
/>
</LinearLayout>
activity_main
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="bw.com.bw_day01_xutils.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:id="@+id/tv_id"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="点我"
android:id="@+id/but_01"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="获取网络数据"
android:id="@+id/but_02"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="下载apk, 显示进度值, 自动安装"
android:id="@+id/but_03"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="数据库"
android:id="@+id/but_04"/>
</LinearLayout>
item_lv
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp">
<ImageView
android:layout_width="200dp"
android:layout_height="200dp"
android:id="@+id/iv_id"
android:src="@mipmap/ic_launcher"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="标题"
android:textSize="30sp"
android:id="@+id/tv_id"/>
</LinearLayout>