关闭

Android:AsyncTask

117人阅读 评论(0) 收藏 举报
分类:
public class MainActivity extends Activity implements OnClickListener {

	private ORMLiteDatabaseHelper mDatabaseHelper;
	private Dao<MyClass, Integer> mClassDao;
	private Dao<Student, Integer> mStudentDao;
	private boolean temp;
	ListView list_view;
	EditText edit;
	Object ob;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		list_view = (ListView) findViewById(R.id.list_view);
		Button create = (Button) findViewById(R.id.create);
		Button query = (Button) findViewById(R.id.query);
		edit = (EditText) findViewById(R.id.edit);

		create.setOnClickListener(this);
		query.setOnClickListener(this);

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.create:
			temp = true;
			MyAsyncTask mAsyncTask = new MyAsyncTask();
			mAsyncTask.execute();//执行语句

			break;
		case R.id.query:
			Integer id = Integer.parseInt(edit.getText().toString());
			temp = false;
			if (id <= 3 && id >= 1) {
				MyAsyncTask mTask = new MyAsyncTask();
				mTask.execute(id);
			}else{
				Toast.makeText(this, "班级不存在", Toast.LENGTH_SHORT).show();
			}
			break;

		default:
			break;
		}
	}

	public class MyAsyncTask extends AsyncTask {

		public MyAsyncTask() {

		}
		//doInBackground 繁琐事件多数在这里执行处理
		@Override
		protected List<Student> doInBackground(Object... params) {

			if (temp) {
				create();
				return null;
			} else {
				ForeignCollection<Student> student = query((Integer) params[0]);
				List<Student> result = new ArrayList<Student>();
				for (Student stu : student) {
					result.add(stu);
				}
				return result;
			}

		}
	//onPostExecute 可直接设置UI界面
		@Override
		protected void onPostExecute(Object result) {

			if (result == null) {
				Toast.makeText(MainActivity.this, "创建数据库成功", Toast.LENGTH_SHORT).show();
			} else {
				Toast.makeText(MainActivity.this, "查询成功", Toast.LENGTH_SHORT).show();

				List<Student> list = (List<Student>) result;
				list_view.setAdapter(new ArrayAdapter(MainActivity.this, android.R.layout.simple_list_item_1, list));
			}
		}
	}

	public void create() {

		mDatabaseHelper = ORMLiteDatabaseHelper.getInstance(this);
		mClassDao = mDatabaseHelper.getClassDao();
		mStudentDao = mDatabaseHelper.getStudentDao();

		for (int i = 1; i < 4; i++) {

			MyClass myclass = new MyClass();

			myclass.myclass_id = i;
			myclass.name = i + "班";

			try {
				mClassDao.createIfNotExists(myclass);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			String[] studentName = { "王二", "张三", "李四", "刘五", "赵六" };
			String[] studentSex = { "男", "女" };
			Random rand = new Random();
			for (int j = 0; j < 5; j++) {

				Student stu = new Student();

				stu.student_id = j;
				stu.name = studentName[rand.nextInt(5)];
				stu.age = 18 + j;
				stu.sex = studentSex[rand.nextInt(2)];

				stu.mclass = myclass;

				try {
					mStudentDao.createIfNotExists(stu);
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}

	public ForeignCollection<Student> query(Integer id) {
		try {
			MyClass mclass = mClassDao.queryForId(id);

			ForeignCollection<Student> student = mclass.student;

			return student;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:46503次
    • 积分:1462
    • 等级:
    • 排名:千里之外
    • 原创:103篇
    • 转载:6篇
    • 译文:0篇
    • 评论:6条
    文章分类
    最新评论