AsyncTask 异步任务 的用法(并且使用ORMlite关系数据库);

时间:2022-01-28 09:22:51
 package com.lixu.asynctask;

 import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.table.DatabaseTable; @DatabaseTable(tableName = "users")
public class User {
public User() { } @Override
public String toString() {
return "id" + user_id + "," + ",姓名:" + name + ",年龄:" + age + ",性别:" + sex+",班级是:"+aclass;
} public int getUser_id() {
return user_id;
} public void setUser_id(int user_id) {
this.user_id = user_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;
} public String getSex() {
return sex;
} public void setSex(String sex) {
this.sex = sex;
} public AClass getAclass() {
return aclass;
} public void setAclass(AClass aclass) {
this.aclass = aclass;
} public User(int user_id, String name, int age, String sex, AClass aclass) {
super();
this.user_id = user_id;
this.name = name;
this.age = age;
this.sex = sex;
this.aclass = aclass;
} @DatabaseField(id=true,columnName = "user_id")
public int user_id; @DatabaseField(columnName = "name")
public String name; @DatabaseField(columnName = "age")
public int age;
@DatabaseField(columnName = "sex")
public String sex;
@DatabaseField(canBeNull = false, foreign = true, foreignAutoRefresh = true)
public AClass aclass; }
 package com.lixu.asynctask;

 import com.j256.ormlite.dao.ForeignCollection;
import com.j256.ormlite.field.DataType;
import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.field.ForeignCollectionField;
import com.j256.ormlite.table.DatabaseTable; @DatabaseTable(tableName = "classes")
public class AClass { public AClass(long id, String name, ForeignCollection<User> user) {
super();
this.id = id;
this.name = name;
this.user = user;
} @Override
public String toString() {
return "id" + id + "班级名:" + name;
} public long getId() {
return id;
} public void setId(long id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public ForeignCollection<User> getUser() {
return user;
} public void setUser(ForeignCollection<User> user) {
this.user = user;
} @DatabaseField(id = true)
public long id; @DatabaseField(dataType = DataType.STRING)
public String name; @ForeignCollectionField(eager = false)
public ForeignCollection<User> user = null; public AClass() { } }
 package com.lixu.asynctask;

 import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.util.Log; public class MyORMlite extends OrmLiteSqliteOpenHelper {
private static String DATABASENAME = "lixu.db";
private static int DATABASEVERSION = 1; private static MyORMlite mMyORMlite = null;
private Dao<User, Integer> muserDao = null;
private Dao<AClass, Integer> mclassDao = null; public MyORMlite(Context context, String databaseName, CursorFactory factory, int databaseVersion) {
super(context, databaseName, factory, databaseVersion);
} public static MyORMlite getInstance(Context context) {
if (mMyORMlite == null) {
mMyORMlite = new MyORMlite(context, DATABASENAME, null, DATABASEVERSION);
}
return mMyORMlite; } @Override
public void onCreate(SQLiteDatabase arg0, ConnectionSource arg1) { try {
TableUtils.createTableIfNotExists(arg1, User.class);
TableUtils.createTableIfNotExists(arg1, AClass.class);
Log.d("MyORMlite", "被创建");
} catch (Exception e) {
e.printStackTrace();
}
} public Dao<User, Integer> getuserDao() {
if (muserDao == null) {
try {
muserDao = getDao(User.class);
} catch (Exception e) {
e.printStackTrace();
}
}
return muserDao;
} public Dao<AClass, Integer> getclassDao() {
if (mclassDao == null) {
try {
mclassDao = getDao(AClass.class);
} catch (Exception e) {
e.printStackTrace();
}
}
return mclassDao;
} @Override
public void close() {
super.close();
muserDao = null;
mclassDao = null; } @Override
public void onUpgrade(SQLiteDatabase arg0, ConnectionSource arg1, int arg2, int arg3) { } }
 package com.lixu.asynctask;

 import java.sql.SQLException;
import java.util.List; import com.j256.ormlite.dao.Dao;
import com.lixu.asynctask.AClass;
import com.lixu.asynctask.User;
import com.lixu.asynctask.MyORMlite; import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast; public class MainActivity extends Activity {
private Dao<User, Integer> muserDao = null;
private Dao<AClass, Integer> mclassDao = null;
TextView tv;
StringBuffer sb = null; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); MyORMlite mMyORMlite = MyORMlite.getInstance(this);
muserDao = mMyORMlite.getuserDao();
mclassDao = mMyORMlite.getclassDao(); tv = (TextView) findViewById(R.id.tv);
tv.setMovementMethod(ScrollingMovementMethod.getInstance()); Button btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
MyTask mMyTask = new MyTask();
mMyTask.execute();
}
});
} // 用一个类继承AsyncTask
private class MyTask extends AsyncTask {
// 先执行这个方法 可以处理一些初始化的事件等。
@Override
protected void onPreExecute() {
sb = new StringBuffer();
super.onPreExecute();
} // 这个方法可以处理相对耗时较长的任务,这个方法里面不可以执行ui任务。
@Override
protected Object doInBackground(Object... params) {
add();
List<User> list = chaxun();
for (User n : list) {
sb.append("姓名是:" + n.getName() + ",性别是:" + n.getSex() + ",班级是:" + n.getAclass().getName() + "\n");
}
return sb;
} // 可以将上一个方法处理的结果传到这个方法中,并且这个方法里面可以处理ui任务。
@Override
protected void onPostExecute(Object params) {
super.onPostExecute(params);
tv.setText(params.toString());
Toast.makeText(MainActivity.this, "显示成功", 0).show();
}
} public List<User> chaxun() {
List<User> list = null;
try {
list = muserDao.queryForAll(); } catch (SQLException e) {
e.printStackTrace();
}
return list; } public void add() { for (int j = 1; j <= 3; j++) {
AClass classes = null;
classes = new AClass();
classes.setName("大三" + j + "班");
classes.setId(j);
try {
mclassDao.createIfNotExists(classes);
} catch (SQLException e) {
e.printStackTrace();
}
for (int i = 10 * (j - 1) + 1; i <= 10 * j; i++) {
User user = new User();
user.setName("张三" + i);
user.setAge(20 + i);
user.setUser_id(i);
user.setSex("男");
user.setAclass(classes);// 挂载到class
try {
muserDao.createIfNotExists(user);
} catch (SQLException e) {
e.printStackTrace();
} }
} }
}