xUtils相关应用
作者:互联网
xUtils相关应用
初始化类
注:元注解不可以删除
package com.example.acer.foremonthjob;
import android.app.Application;
import org.xutils.x;
public class App extends Application {
@Override
public void onCreate() {
super.onCreate();
//初始化xutils
x.Ext.init(this);
}
}
主类下载图片
布局文件
<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:orientation="vertical"
android:layout_height="match_parent"
tools:context=".Demo">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="下载图片"
android:id="@+id/downimg"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="下载json"
android:id="@+id/downjson"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="数据库"
android:id="@+id/database"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="下载apk"
android:id="@+id/downapk"
/>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/img"
/>
</LinearLayout>
代码
@ContentView(R.layout.activity_demo)
public class Demo extends AppCompatActivity {
@ViewInject(R.id.img)
private ImageView img;
//图片网址
String url = "http://pic27.nipic.com/20130329/890845_115317964000_2.jpg";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_demo);
//绑定控件和activity
x.view().inject(this);
}
//添加点击事件
@Event(value = {R.id.downapk,R.id.downjson,R.id.database,R.id.downimg},type = View.OnClickListener.class)
private void click(View v){
//判断事件跳转界面
switch (v.getId()){
case R.id.downapk:
Intent intent = new Intent(Demo.this, DownApk.class);
startActivity(intent);
break;
case R.id.downjson:
Intent intent1 = new Intent(Demo.this, DownJson.class);
startActivity(intent1);
break;
case R.id.database:
Intent intent2 = new Intent(Demo.this, DBActivity.class);
startActivity(intent2);
break;
case R.id.downimg:
x.image().bind(img,url);
break;
}
}
}
效果图
下载json串
布局文件
<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=".DownJson">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="下载"
android:id="@+id/down"
/>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/list"
></ListView>
</LinearLayout>
代码
@ContentView(R.layout.activity_down_json)
public class DownJson extends AppCompatActivity {
String url = "http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=10&page=1";
@ViewInject(R.id.list)
private ListView list;
ArrayList<JsonBean> lists = new ArrayList<>();
MyAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_down_json);
//绑定
x.view().inject(this);
//初始化适配器
initAdapter();
}
private void initAdapter() {
adapter = new MyAdapter(this,lists);
list.setAdapter(adapter);
}
//点击事件
@Event(value = R.id.down,type = View.OnClickListener.class)
private void click(View view){
RequestParams params = new RequestParams();
params.setUri(url);
x.http().get(params, new Callback.CommonCallback<String>() {
@Override
public void onSuccess(String result) {
//调用解析方法
json(result);
}
@Override
public void one rror(Throwable ex, boolean isOnCallback) {
}
@Override
public void onCancelled(CancelledException cex) {
}
@Override
public void onFinished() {
}
});
}
private void json(String s) {
try {
JSONObject object = new JSONObject(s);
JSONArray data = object.getJSONArray("data");
for (int i = 0; i < data.length(); i++) {
JSONObject oo = data.getJSONObject(i);
String title = oo.getString("title");
String pic = oo.getString("pic");
JsonBean jsonBean = new JsonBean(title, pic);
lists.add(jsonBean);
}
//刷新适配器
adapter.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
}
}
适配器
class MyAdapter extends BaseAdapter {
Context context;
ArrayList<JsonBean> lists;
public MyAdapter(Context context, ArrayList<JsonBean> lists) {
this.context = context;
this.lists = lists;
}
@Override
public int getCount() {
return lists.size();
}
@Override
public Object getItem(int position) {
return lists.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final MyHolder holder;
if(convertView == null){
holder = new MyHolder();
convertView = View.inflate(context,R.layout.item,null);
holder.img = convertView.findViewById(R.id.imgs);
holder.name = convertView.findViewById(R.id.name);
convertView.setTag(holder);
}else{
holder = (MyHolder) convertView.getTag();
}
holder.name.setText(lists.get(position).name);
Picasso.with(context).load(lists.get(position).pic).into(holder.img);
return convertView;
}
class MyHolder{
ImageView img;
TextView name;
}
}
效果图
下载apk
布局
<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=".DownApk">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="下载apk"
android:id="@+id/down_apk"
/>
</android.support.constraint.ConstraintLayout>
代码
@ContentView(R.layout.activity_down_apk)
public class DownApk extends AppCompatActivity {
Callback.Cancelable cancelable;
ProgressDialog dialog;
String url = "http://softfile.3g.qq.com:8080/msoft/179/24659/43549/qq_hd_mini_1.4.apk";
String path = Environment.getExternalStorageDirectory()+"/day.apk";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_down_apk);
//绑定
x.view().inject(this);
//初始化dialog
initProg();
}
private void initProg() {
dialog = new ProgressDialog(this);
dialog.setTitle("下载apk");
dialog.setMessage("下载中");
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
dialog.setCanceledOnTouchOutside(false);
//设置暂停按钮
dialog.setButton(ProgressDialog.BUTTON_NEGATIVE, "暂停", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//关闭
cancelable.cancel();
}
});
}
//点击事件
@Event(value = R.id.down_apk,type = View.OnClickListener.class)
private void click(View view){
RequestParams params = new RequestParams();
params.setUri(url);
//设置存储路径
params.setSaveFilePath(path);
params.setCancelFast(true);
params.setAutoRename(true);
cancelable = x.http().get(params, new Callback.ProgressCallback<File>() {
@Override
public void onSuccess(File result) {
Toast.makeText(DownApk.this, "下载成功" + path, Toast.LENGTH_SHORT).show();
//自动安装
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(result),"application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
DownApk.this.startActivity(intent);
}
@Override
public void one rror(Throwable ex, boolean isOnCallback) {
}
@Override
public void onCancelled(CancelledException cex) {
}
@Override
public void onFinished() {
//下载完成关闭dialog
dialog.cancel();
}
@Override
public void onWaiting() {
}
@Override
public void onStarted() {
//开始下载
dialog.show();
}
@Override
public void onl oading(long total, long current, boolean isDownloading) {
//设置进度
dialog.setProgress((int) (current*100/total));
}
});
}
}
效果图
数据库添加查询
布局
<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=".DBActivity">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="增加"
android:id="@+id/add"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/select"
android:text="查询"
/>
</LinearLayout>
代码
@ContentView(R.layout.activity_db)
public class DBActivity extends AppCompatActivity {
DbManager.DaoConfig config;
DbManager db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_db);
x.view().inject(this);
initDB();
}
private void initDB() {
config = new DbManager.DaoConfig();
config.setDbName("user");
db = x.getDb(config);
}
@Event(value = {R.id.add,R.id.select},type = View.OnClickListener.class)
private void click(View view){
switch (view.getId()){
case R.id.add:
addUser();
break;
case R.id.select:
selectUser();
break;
}
}
private void selectUser() {
try {
UserBean first = db.selector(UserBean.class).findFirst();
Toast.makeText(this, first.toString(), Toast.LENGTH_SHORT).show();
} catch (DbException e) {
e.printStackTrace();
}
}
private void addUser() {
UserBean bean = new UserBean();
bean.setId(2);
bean.setAge(19);
bean.setName("蛋蛋");
try {
db.save(bean);
} catch (DbException e) {
e.printStackTrace();
}
}
}
效果图
标签:void,id,xUtils,应用,new,Override,相关,class,public 来源: https://blog.csdn.net/wangwei_weibo/article/details/94758764