1 package com.lixu.asynctask; 2 3 import com.j256.ormlite.field.DatabaseField; 4 import com.j256.ormlite.table.DatabaseTable; 5 6 @DatabaseTable(tableName = "users") 7 public class User { 8 public User() { 9 10 } 11 12 13 14 15 @Override 16 public String toString() { 17 return "id" + user_id + "," + ",姓名:" + name + ",年龄:" + age + ",性别:" + sex+",班级是:"+aclass; 18 } 19 20 21 22 23 public int getUser_id() { 24 return user_id; 25 } 26 27 public void setUser_id(int user_id) { 28 this.user_id = user_id; 29 } 30 31 public String getName() { 32 return name; 33 } 34 35 public void setName(String name) { 36 this.name = name; 37 } 38 39 public int getAge() { 40 return age; 41 } 42 43 public void setAge(int age) { 44 this.age = age; 45 } 46 47 public String getSex() { 48 return sex; 49 } 50 51 public void setSex(String sex) { 52 this.sex = sex; 53 } 54 55 public AClass getAclass() { 56 return aclass; 57 } 58 59 public void setAclass(AClass aclass) { 60 this.aclass = aclass; 61 } 62 63 public User(int user_id, String name, int age, String sex, AClass aclass) { 64 super(); 65 this.user_id = user_id; 66 this.name = name; 67 this.age = age; 68 this.sex = sex; 69 this.aclass = aclass; 70 } 71 72 73 74 @DatabaseField(id=true,columnName = "user_id") 75 public int user_id; 76 77 @DatabaseField(columnName = "name") 78 public String name; 79 80 @DatabaseField(columnName = "age") 81 public int age; 82 @DatabaseField(columnName = "sex") 83 public String sex; 84 @DatabaseField(canBeNull = false, foreign = true, foreignAutoRefresh = true) 85 public AClass aclass; 86 87 }
1 package com.lixu.asynctask; 2 3 import com.j256.ormlite.dao.ForeignCollection; 4 import com.j256.ormlite.field.DataType; 5 import com.j256.ormlite.field.DatabaseField; 6 import com.j256.ormlite.field.ForeignCollectionField; 7 import com.j256.ormlite.table.DatabaseTable; 8 9 @DatabaseTable(tableName = "classes") 10 public class AClass { 11 12 public AClass(long id, String name, ForeignCollection<User> user) { 13 super(); 14 this.id = id; 15 this.name = name; 16 this.user = user; 17 } 18 19 @Override 20 public String toString() { 21 return "id" + id + "班级名:" + name; 22 } 23 24 public long getId() { 25 return id; 26 } 27 28 public void setId(long id) { 29 this.id = id; 30 } 31 32 public String getName() { 33 return name; 34 } 35 36 public void setName(String name) { 37 this.name = name; 38 } 39 40 public ForeignCollection<User> getUser() { 41 return user; 42 } 43 44 public void setUser(ForeignCollection<User> user) { 45 this.user = user; 46 } 47 48 @DatabaseField(id = true) 49 public long id; 50 51 @DatabaseField(dataType = DataType.STRING) 52 public String name; 53 54 @ForeignCollectionField(eager = false) 55 public ForeignCollection<User> user = null; 56 57 public AClass() { 58 59 } 60 61 }
1 package com.lixu.asynctask; 2 3 import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper; 4 import com.j256.ormlite.dao.Dao; 5 import com.j256.ormlite.support.ConnectionSource; 6 import com.j256.ormlite.table.TableUtils; 7 import android.content.Context; 8 import android.database.sqlite.SQLiteDatabase; 9 import android.database.sqlite.SQLiteDatabase.CursorFactory; 10 import android.util.Log; 11 12 public class MyORMlite extends OrmLiteSqliteOpenHelper { 13 private static String DATABASENAME = "lixu.db"; 14 private static int DATABASEVERSION = 1; 15 16 private static MyORMlite mMyORMlite = null; 17 private Dao<User, Integer> muserDao = null; 18 private Dao<AClass, Integer> mclassDao = null; 19 20 public MyORMlite(Context context, String databaseName, CursorFactory factory, int databaseVersion) { 21 super(context, databaseName, factory, databaseVersion); 22 } 23 24 public static MyORMlite getInstance(Context context) { 25 if (mMyORMlite == null) { 26 mMyORMlite = new MyORMlite(context, DATABASENAME, null, DATABASEVERSION); 27 } 28 return mMyORMlite; 29 30 } 31 32 @Override 33 public void onCreate(SQLiteDatabase arg0, ConnectionSource arg1) { 34 35 try { 36 TableUtils.createTableIfNotExists(arg1, User.class); 37 TableUtils.createTableIfNotExists(arg1, AClass.class); 38 Log.d("MyORMlite", "被创建"); 39 } catch (Exception e) { 40 e.printStackTrace(); 41 } 42 } 43 44 public Dao<User, Integer> getuserDao() { 45 if (muserDao == null) { 46 try { 47 muserDao = getDao(User.class); 48 } catch (Exception e) { 49 e.printStackTrace(); 50 } 51 } 52 return muserDao; 53 } 54 55 public Dao<AClass, Integer> getclassDao() { 56 if (mclassDao == null) { 57 try { 58 mclassDao = getDao(AClass.class); 59 } catch (Exception e) { 60 e.printStackTrace(); 61 } 62 } 63 return mclassDao; 64 } 65 66 @Override 67 public void close() { 68 super.close(); 69 muserDao = null; 70 mclassDao = null; 71 72 } 73 74 @Override 75 public void onUpgrade(SQLiteDatabase arg0, ConnectionSource arg1, int arg2, int arg3) { 76 77 } 78 79 }
1 package com.lixu.asynctask; 2 3 import java.sql.SQLException; 4 import java.util.List; 5 6 import com.j256.ormlite.dao.Dao; 7 import com.lixu.asynctask.AClass; 8 import com.lixu.asynctask.User; 9 import com.lixu.asynctask.MyORMlite; 10 11 import android.app.Activity; 12 import android.os.AsyncTask; 13 import android.os.Bundle; 14 import android.text.method.ScrollingMovementMethod; 15 import android.view.View; 16 import android.view.View.OnClickListener; 17 import android.widget.Button; 18 import android.widget.TextView; 19 import android.widget.Toast; 20 21 public class MainActivity extends Activity { 22 private Dao<User, Integer> muserDao = null; 23 private Dao<AClass, Integer> mclassDao = null; 24 TextView tv; 25 StringBuffer sb = null; 26 27 @Override 28 protected void onCreate(Bundle savedInstanceState) { 29 super.onCreate(savedInstanceState); 30 setContentView(R.layout.activity_main); 31 32 MyORMlite mMyORMlite = MyORMlite.getInstance(this); 33 muserDao = mMyORMlite.getuserDao(); 34 mclassDao = mMyORMlite.getclassDao(); 35 36 tv = (TextView) findViewById(R.id.tv); 37 tv.setMovementMethod(ScrollingMovementMethod.getInstance()); 38 39 Button btn = (Button) findViewById(R.id.button1); 40 btn.setOnClickListener(new OnClickListener() { 41 @Override 42 public void onClick(View v) { 43 MyTask mMyTask = new MyTask(); 44 mMyTask.execute(); 45 } 46 }); 47 } 48 49 // 用一个类继承AsyncTask 50 private class MyTask extends AsyncTask { 51 // 先执行这个方法 可以处理一些初始化的事件等。 52 @Override 53 protected void onPreExecute() { 54 sb = new StringBuffer(); 55 super.onPreExecute(); 56 } 57 58 // 这个方法可以处理相对耗时较长的任务,这个方法里面不可以执行ui任务。 59 @Override 60 protected Object doInBackground(Object... params) { 61 add(); 62 List<User> list = chaxun(); 63 for (User n : list) { 64 sb.append("姓名是:" + n.getName() + ",性别是:" + n.getSex() + ",班级是:" + n.getAclass().getName() + "\n"); 65 } 66 return sb; 67 } 68 69 // 可以将上一个方法处理的结果传到这个方法中,并且这个方法里面可以处理ui任务。 70 @Override 71 protected void onPostExecute(Object params) { 72 super.onPostExecute(params); 73 tv.setText(params.toString()); 74 Toast.makeText(MainActivity.this, "显示成功", 0).show(); 75 } 76 } 77 78 public List<User> chaxun() { 79 List<User> list = null; 80 try { 81 list = muserDao.queryForAll(); 82 83 } catch (SQLException e) { 84 e.printStackTrace(); 85 } 86 return list; 87 88 } 89 90 public void add() { 91 92 for (int j = 1; j <= 3; j++) { 93 AClass classes = null; 94 classes = new AClass(); 95 classes.setName("大三" + j + "班"); 96 classes.setId(j); 97 try { 98 mclassDao.createIfNotExists(classes); 99 } catch (SQLException e) { 100 e.printStackTrace(); 101 } 102 for (int i = 10 * (j - 1) + 1; i <= 10 * j; i++) { 103 User user = new User(); 104 user.setName("张三" + i); 105 user.setAge(20 + i); 106 user.setUser_id(i); 107 user.setSex("男"); 108 user.setAclass(classes);// 挂载到class 109 try { 110 muserDao.createIfNotExists(user); 111 } catch (SQLException e) { 112 e.printStackTrace(); 113 } 114 115 } 116 } 117 118 } 119 }
AsyncTask 异步任务 的用法(并且使用ORMlite关系数据库);
原文:http://www.cnblogs.com/labixiaoxin/p/4959727.html