Android:AsyncTask

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;
	}

}

你可能感兴趣的:(Android:AsyncTask)