4】包com.andyidea.db下DatabaseHelper.java源码:

  1. packagecom.andyidea.db;
  2. importjava.sql.SQLException;
  3. importAndroid.content.Context;
  4. importAndroid.database.sqlite.SQLiteDatabase;
  5. importAndroid.util.Log;
  6. importcom.andyidea.bean.Student;
  7. importcom.j256.ormlite.Android.apptools.OrmLiteSqliteOpenHelper;
  8. importcom.j256.ormlite.dao.Dao;
  9. importcom.j256.ormlite.support.ConnectionSource;
  10. importcom.j256.ormlite.table.TableUtils;
  11. publicclassDatabaseHelperextendsOrmLiteSqliteOpenHelper{
  12. privatestaticfinalStringDATABASE_NAME="ormlite.db";
  13. privatestaticfinalintDATABASE_VERSION=1;
  14. privateDao<Student,Integer>stuDao=null;
  15. publicDatabaseHelper(Contextcontext){
  16. super(context,DATABASE_NAME,null,DATABASE_VERSION);
  17. }
  18. /**
  19. *创建SQLite数据库
  20. */
  21. @Override
  22. publicvoidonCreate(SQLiteDatabasesqliteDatabase,ConnectionSourceconnectionSource){
  23. try{
  24. TableUtils.createTable(connectionSource,Student.class);
  25. }catch(SQLExceptione){
  26. Log.e(DatabaseHelper.class.getName(),"Unabletocreatedatbases",e);
  27. }
  28. }
  29. /**
  30. *更新SQLite数据库
  31. */
  32. @Override
  33. publicvoidonUpgrade(
  34. SQLiteDatabasesqliteDatabase,
  35. ConnectionSourceconnectionSource,
  36. intoldVer,
  37. intnewVer){
  38. try{
  39. TableUtils.dropTable(connectionSource,Student.class,true);
  40. onCreate(sqliteDatabase,connectionSource);
  41. }catch(SQLExceptione){
  42. Log.e(DatabaseHelper.class.getName(),
  43. "Unabletoupgradedatabasefromversion"+oldVer+"tonew"
  44. +newVer,e);
  45. }
  46. }
  47. publicDao<Student,Integer>getStudentDao()throwsSQLException{
  48. if(stuDao==null){
  49. stuDao=getDao(Student.class);
  50. }
  51. returnstuDao;
  52. }
  53. }
【5】包com.andyidea.ormsqlite下源码:

MainActivity.java源码:

[html]
  1. packagecom.andyidea.ormsqlite;
  2. importjava.sql.SQLException;
  3. importcom.andyidea.bean.Student;
  4. importcom.andyidea.db.DatabaseHelper;
  5. importcom.j256.ormlite.Android.apptools.OrmLiteBaseActivity;
  6. importcom.j256.ormlite.dao.Dao;
  7. importAndroid.content.Intent;
  8. importAndroid.os.Bundle;
  9. importAndroid.view.Menu;
  10. importAndroid.view.MenuItem;
  11. importAndroid.widget.EditText;
  12. publicclassMainActivityextendsOrmLiteBaseActivity<DatabaseHelper>{
  13. privateEditTextstuNO;
  14. privateEditTextstuName;
  15. privateEditTextstuAge;
  16. privateEditTextstuSex;
  17. privateEditTextstuScore;
  18. privateEditTextstuAddress;
  19. privateStudentmStudent;
  20. privateDao<Student,Integer>stuDao;
  21. privatefinalintMENU_ADD=Menu.FIRST;
  22. privatefinalintMENU_VIEWALL=Menu.FIRST+1;
  23. privatefinalintMENU_EDIT=Menu.FIRST+2;
  24. privateBundlemBundle=newBundle();
  25. /**Calledwhentheactivityisfirstcreated.*/
  26. @Override
  27. publicvoidonCreate(BundlesavedInstanceState){
  28. super.onCreate(savedInstanceState);
  29. setContentView(R.layout.main);
  30. initializeViews();
  31. }
  32. /**
  33. *初始化UI界面
  34. */
  35. privatevoidinitializeViews(){
  36. stuNO=(EditText)findViewById(R.id.stuno);
  37. stuName=(EditText)findViewById(R.id.name);
  38. stuAge=(EditText)findViewById(R.id.age);
  39. stuSex=(EditText)findViewById(R.id.sex);
  40. stuScore=(EditText)findViewById(R.id.score);
  41. stuAddress=(EditText)findViewById(R.id.address);
  42. mBundle=getIntent().getExtras();
  43. if(mBundle!=null&&mBundle.getString("action").equals("viewone")){
  44. mStudent=(Student)getIntent().getSerializableExtra("entity");
  45. setStudentUIData(mStudent);
  46. }
  47. if(mBundle!=null&&mBundle.getString("action").equals("edit")){
  48. mStudent=(Student)getIntent().getSerializableExtra("entity");
  49. setStudentUIData(mStudent);
  50. }
  51. }
  52. @Override
  53. publicbooleanonPrepareOptionsMenu(Menumenu){
  54. if(mBundle!=null&&mBundle.getString("action").equals("viewone"))
  55. returnfalse;
  56. else
  57. returnsuper.onPrepareOptionsMenu(menu);
  58. }
  59. @Override
  60. publicbooleanonCreateOptionsMenu(Menumenu){
  61. if(mBundle!=null&&mBundle.getString("action").equals("edit")){
  62. menu.add(1,MENU_EDIT,0,"保存");
  63. }else{
  64. menu.add(0,MENU_ADD,0,"增加");
  65. menu.add(0,MENU_VIEWALL,0,"查看");
  66. }
  67. returnsuper.onCreateOptionsMenu(menu);
  68. }
  69. @Override
  70. publicbooleanonOptionsItemSelected(MenuItemitem){
  71. switch(item.getItemId()){
  72. caseMENU_ADD:
  73. try{
  74. stuDao=getHelper().getStudentDao();
  75. getStudentData();
  76. if(mStudent!=null){
  77. //创建记录项
  78. stuDao.create(mStudent);
  79. }
  80. }catch(SQLExceptione){
  81. e.printStackTrace();
  82. }
  83. break;
  84. caseMENU_VIEWALL:
  85. Intentintent=newIntent();
  86. intent.setClass(MainActivity.this,StudentListActivity.class);
  87. startActivity(intent);
  88. break;
  89. caseMENU_EDIT:
  90. try{
  91. getStudentData();
  92. stuDao=getHelper().getStudentDao();
  93. if(mStudent!=null){
  94. //更新某记录项
  95. stuDao.update(mStudent);
  96. }
  97. }catch(SQLExceptione){
  98. e.printStackTrace();
  99. }
  100. break;
  101. default:
  102. break;
  103. }
  104. returnsuper.onOptionsItemSelected(item);
  105. }
  106. /**
  107. *获取界面值(实体信息)
  108. */
  109. privatevoidgetStudentData(){
  110. mStudent=newStudent();
  111. mStudent.setStuNO(stuNO.getText().toString());
  112. mStudent.setName(stuName.getText().toString());
  113. mStudent.setAge(Integer.parseInt(stuAge.getText().toString()));
  114. mStudent.setSex(stuSex.getText().toString());
  115. mStudent.setScore(Double.parseDouble(stuScore.getText().toString()));
  116. mStudent.setAddress(stuAddress.getText().toString());
  117. }
  118. /**
  119. *赋值给UI界面
  120. *@paramstudent
  121. */
  122. privatevoidsetStudentUIData(Studentstudent){
  123. stuNO.setText(student.getStuNO());
  124. stuName.setText(student.getName());
  125. stuAge.setText(String.valueOf(student.getAge()));
  126. stuSex.setText(student.getSex());
  127. stuScore.setText(String.valueOf(student.getScore()));
  128. stuAddress.setText(student.getAddress());
  129. }
  130. }

更多相关文章

  1. android 界面应用锦集
  2. Android使用DrawLayout,ToolBar和ActionBarDrawerToggle实现抽屉
  3. android最新源码下载
  4. Android控件开发之Gallery
  5. okhttp源码学习分析一
  6. android音乐播放器(4)
  7. 音视频转码合成
  8. Android仿苹果关机界面实现代码
  9. Android创建和使用数据库详细指南(7)

随机推荐

  1. Android学习小结
  2. 关于Android(安卓)LCD和键盘背光亮度 .
  3. Android(安卓)编译系统 --- 版本信息
  4. [置顶] Android应用开发 第二讲:Android系
  5. [转载]Android及Robotium学习总结【环境
  6. Android RIL 架构
  7. Android 4.4 (KitKat) SMS Apis Change—
  8. 深入剖析Android消息机制
  9. Android 应用程序消息处理机制(Looper、Ha
  10. Windows8下PhoneGap 4 + Android(安卓)St