【Android源码 栏目提醒】:网学会员在Android源码 频道为大家收集整理了“Android开发之学生信息管理系统 - 其它资料“提供大家参考,希望对大家有所帮助!
.
Android 开发之学生信息管理系统 复制链接今天上课老师让利用 ListView 和数据库做一个学员信息管理系统。
下面我就把自己做的代码复制下来,供大家参考。
首页的布局 main.xml01.ltxml versionquot1.0quot encodingquotutf-8quotgt02.ltLinearLayout xmlns:androidquothttp://schemas.
android.com/apk/res/androidquot03.
android:orientationquotverticalquot
android:layout_widthquotfill_parentquot04.
android:layout_heightquotfill_parentquotgt05. ltRelativeLayout
android:idquotid/RelativeLayoutquot06.
android:layout_widthquotfill_parentquot07.
android:layout_heightquotwrap_contentquotgt08. ltButton
android:idquotid/bn_search_idquot09.
android:layout_widthquotwrap_contentquot10.
android:layout_heightquotwrap_contentquot11.
android:textquot搜索quot12.
android:gravityquotcenter_verticalquot /gt13. ltButton
android:gravityquotcenterquot14.
android:textquotstring/myButtonquot15.
android:idquotid/btn_add_studentquot16.
android:layout_widthquotwrap_contentquot17.
android:layout_heightquotwrap_contentquot18.
android:layout_alignParentTopquottruequot19.
android:layout_toRightOfquotid/bn_search_idquot20.
android:layout_toLeftOfquotid/bn_selectquot /gt21. ltButton
android:gravityquotcenter_verticalquot22.
android:textquot选择quot23.
android:idquotid/bn_selectquot24.
android:layout_widthquotwrap_contentquot25.
android:layout_heightquotwrap_contentquot26.
android:layout_alignParentTopquottruequot27.
android:layout_alignParentRightquottruequotgtlt/Buttongt28. lt/RelativeLayoutgt29. ltTextView30.
android:layout_widthquotfill_parentquot31.
android:layout_heightquotwrap_contentquot32.
android:gravityquotcenterquot33.
android:textquot ID 姓 名 年 龄 性别 quot34. /gt35. ltListView
android:idquotandroid:id/listquot36.
android:layout_widthquotfill_parentquot37.
android:layout_weightquot1quot38.
android:layout_heightquotwrap_contentquot/gt39. ltLinearLayout40.
android:orientationquothorizontalquot41.
android:idquotid/showLinerquot42.
android:visibilityquotgonequot43.
android:layout_widthquotfill_parentquot44.
android:layout_heightquotwrap_contentquotgt45. ltButton46.
android:idquotid/bn_deletequot47.
android:layout_widthquotfill_parentquot48.
android:layout_heightquotwrap_contentquot49.
android:layout_weightquot1quot50.
android:textquot删除quot51.
android:enabledquotfalsequot52. /gt53. ltButton54.
android:idquotid/bn_selectallquot55.
android:layout_widthquotfill_parentquot56.
android:layout_heightquotwrap_contentquot57.
android:layout_weightquot1quot58.
android:textquot全选quot59. /gt60. ltButton61.
android:idquotid/bn_canelquot62.
android:layout_widthquotfill_parentquot63.
android:layout_heightquotwrap_contentquot64.
android:layout_weightquot1quot65.
android:textquot取消quot66. /gt67. lt/LinearLayoutgt68.69.lt/LinearLayoutgt复制代码 创建 listView 中显示学员信息的 xml 格式 student_list_item.xml01.ltxml versionquot1.0quot encodingquotutf-8quotgt02.ltLinearLayout xmlns:androidquothttp://schemas.
android.com/apk/res/androidquot03.
android:orientationquothorizontalquot04.
android:layout_widthquotfill_parentquot05.
android:layout_heightquotwrap_contentquot gt06. ltImageView
android:layout_widthquotfill_parentquot07.
android:layout_heightquotwrap_contentquot08.
android:layout_gravityquotcenterquot09.
android:layout_weightquot1quot10.
android:backgroundquotdrawable/iconquot/gt11. ltTextView
android:idquotid/tv_stu_idquot12.
android:layout_widthquotfill_parentquot13.
android:layout_gravityquotcenterquot14.
android:layout_heightquotwrap_contentquot15.
android:layout_weightquot1quot/gt16. ltTextView
android:idquotid/tv_stu_namequot17.
android:layout_widthquotfill_parentquot18.
android:layout_gravityquotcenterquot19.
android:layout_heightquotwrap_contentquot20.
android:layout_weightquot1quot/gt21.22. ltTextView
android:idquotid/tv_stu_agequot23.
android:layout_widthquotfill_parentquot24.
android:layout_gravityquotcenterquot25.
android:layout_heightquotwrap_contentquot26.
android:layout_weightquot1quot/gt27. ltTextView
android:idquotid/tv_stu_sexquot28.
android:layout_widthquotfill_parentquot29.
android:layout_gravityquotcenterquot30.
android:layout_heightquotwrap_contentquot31.
android:layout_weightquot1quot/gt32.33. ltTextView
android:idquotid/tv_stu_likesquot34.
android:layout_widthquotfill_parentquot35.
android:layout_gravityquotcenterquot36.
android:layout_heightquotwrap_contentquot37.
android:layout_weightquot1quot38.
android:visibilityquotgonequot/gt39. ltTextView
android:idquotid/tv_stu_phonequot40.
android:layout_widthquotfill_parentquot41.
android:layout_gravityquotcenterquot42.
android:layout_heightquotwrap_contentquot43.
android:layout_weightquot1quot44.
android:visibilityquotgonequot/gt45. ltTextView
android:idquotid/tv_stu_traindatequot46.
android:layout_widthquotfill_parentquot47.
android:layout_heightquotwrap_contentquot48.
android:layout_gravityquotcenterquot49.
android:layout_weightquot1quot50.
android:visibilityquotgonequot/gt51. ltTextView
android:idquotid/tv_stu_modifyDateTimequot52.
android:layout_widthquotfill_parentquot53.
android:layout_gravityquotcenterquot54.
android:layout_heightquotwrap_contentquot55.
android:layout_weightquot1quot56.
android:visibilityquotgonequot/gt57.58. ltCheckBox59.
android:idquotid/cb_boxquot60.
android:layout_widthquotfill_parentquot61.
android:layout_heightquotwrap_contentquot62.
android:layout_weightquot1quot63.
android:visibilityquotgonequot64.
android:checkedquotfalsequot65.
android:focusablequotfalsequot66. /gt67.lt/LinearLayoutgt复制代码创建一个 StudentListActivity 做为主页显示学员信息以及进行一些操作。
01.package cn.yj3g.student.activity02.03.import java.util.ArrayList04.import java.util.Iterator05.import java.util.List06.07.import
android.app.AlertDialog08.import
android.app.ListActivity09.import
android.content.DialogInterface10.import
android.content.Intent11.import
android.database.Cursor12.import
android.database.sqlite.SQLiteDatabase13.import
android.os.Bundle14.import
android.util.Log15.import
android.view.ContextMenu16.import
android.view.ContextMenu.ContextMenuInfo17.import
android.view.Menu18.import
android.view.MenuInflater19.import
android.view.MenuItem20.import
android.view.View21.import
android.view.View.OnClickListener22.import
android.widget.AdapterView23.import
android.widget.AdapterView.OnItemClickListener24.import
android.widget.AdapterView.OnItemLongClickListener25.import
android.widget.Button26.import
android.widget.CheckBox27.import
android.widget.LinearLayout28.import
android.widget.ListView29.import
android.widget.RelativeLayout30.import
android.widget.SimpleCursorAdapter31.import
android.widget.Toast32.import cn.yj3g.student.dao.StudentDao33.import cn.yj3g.student.db.StudentDBHelper34.import cn.yj3g.student.entry.Student35.import cn.yj3g.student.entry.TableContanst36.37.public class StudentListActivity extends ListActivity implements38. OnClickListener OnItemClickListener OnItemLongClickListener 39.40. private static final String TAG quotTestSQLitequot41. private Button addStudent42. private Cursor cursor43. private SimpleCursorAdapter adapter44. private ListView listView45. private ListltLonggt list46. private RelativeLayout relativeLayout47. private Button searchButton48. private Button selectButton49. private Button deleteButton50. private Button selectAllButton51. private Button canleButton52. private LinearLayout layout53. private StudentDao dao54. private Student student55. private Boolean isDeleteList false56.57. Override58. public void onCreateBundle savedInstanceState 59. super.onCreatesavedInstanceState60. setContentViewR.layout.main61. Log.eTAG quotonCreatequot62. list new ArrayListltLonggt63. student new Student64. dao new StudentDaonew StudentDBHelperthis65. addStudent Button findViewByIdR.id.btn_add_student66. searchButton Button findViewByIdR.id.bn_search_id67. selectButton Button findViewByIdR.id.bn_select68. deleteButton Button findViewByIdR.id.bn_delete69. selectAllButton Button findViewByIdR.id.bn_selectall70. canleButton Button findViewByIdR.id.bn_canel71. layout LinearLayout findViewByIdR.id.showLiner72. relativeLayoutRelativeLayout findViewByIdR.id.RelativeLayout73. listView getListView74.75. // 为按键设置监听76. addStudent.setOnClickListenerthis77. searchButton.setOnClickListenerthis78. selectButton.setOnClickListenerthis79. deleteButton.setOnClickListenerthis80. canleButton.setOnClickListenerthis81. selectAllButton.setOnClickListenerthis82. listView.setOnItemClickListenerthis83. listView.setOnItemLongClickListenerthis84. listView.setOnCreateContextMenuListenerthis85.86. 87.88. Override89. protected void onStart 90. // 调用 load方法将数据库中的所有记录显示在当前页面91. super.onStart92. load93.94. 95.96. public void onClickView v 97. // 跳转到添加信息的界面98. if v addStudent 99. startActivitynew Intentthis AddStudentActivity.class100. else if v searchButton 101. // 跳转到查询界面102. startActivitynew Intentthis StudentSearch.class103. else if v selectButton 104. // 跳转到选择界面105. isDeleteList isDeleteList106. if isDeleteList 107. checkOrClearAllCheckboxstrue108. else 109. showOrHiddenCheckBoxsfalse110. 111. else if v deleteButton 112. // 删除数据113. if list.size gt 0 114. for int i 0 i lt list.size i 115. long id list.geti116. Log.eTAG quotdelete idquot id117. int count dao.deleteStudentByIdid118. 119. dao.closeDB120. load121. 122. else if v canleButton 123. // 点击取消,回到初始界面124. load125. layout.setVisibilityView.GONE126. isDeleteList isDeleteList127. else if v selectAllButton 128. // 全选,如果当前全选按钮显示是全选,则在点击后变为取消全选,如果当前为取消全选,则在点击后变为全选129. selectAllMethods130. 131. 132. // 创建菜单133. Override134. public void onCreateContextMenuContextMenu menu View v135. ContextMenuInfo menuInfo 136. MenuInflater inflater getMenuInflater137. inflater.inflateR.layout.menu menu138. 139.140. // 对菜单中的按钮添加响应时间141. Override142. public boolean onContextItemSelectedMenuItem item 143. int item_id item.getItemId144. student Student listView.getTag145. Log.vTAG quotTestSQLitestudentquot listView.getTag quotquot146. final long student_id student.getId147. Intent intent new Intent148. // Log.vTAG quotTestSQLiteidquotstudent_id149. switch item_id 150. // 添加151. case R.id.add:152. startActivitynew Intentthis AddStudentActivity.class153. break154. // 删除155. case R.id.delete:156. deleteStudentInformationstudent_id157. break158. case R.id.look:159. // 查看学生信息160. // Log.vTAG quotTestSQLitelookquotstudentquotquot161. intent.putExtraquotstudentquot student162. intent.setClassthis ShowStudentActivity.class163. this.startActivityintent164. break165. case R.id.write:166. // 修改学生信息167. intent.putExtraquotstudentquot student168. intent.setClassthis AddStudentActivity.class169. this.startActivityintent170. break171. default:172. break173. 174. return super.onContextItemSelecteditem175. 176.177. // 创建一个按钮菜单178. Override179. public boolean onCreateOptionsMenuMenu menu 180. menu.add1 1 1 quot按入学日期排序quot181. menu.add1 2 1 quot按姓名进行排序quot182. menu.add1 5 1 quot按学号进行排序quot183. menu.add1 3 1 quot模糊查找quot184. menu.add1 4 1 quot退出quot185. return super.onCreateOptionsMenumenu186. 187.188. // 对菜单中的按钮添加响应时间189. Override190. public boolean onOptionsItemSelectedMenuItem item 191. int id item.getItemId192. switch id 193. // 排序194. case 1:195. cursor dao.sortByTrainDate196. loadcursor197. break198.199. // 排序200. case 2:201. cursor dao.sortByName202. loadcursor203. break204. // 查找205. case 3:206. startActivitynew Intentthis StudentSearch.class207. break208. // 退出209. case 4:210. finish211. break212. case 5:213. cursor dao.sortByID214. loadcursor215. break216. default:217. break218. 219. return super.onOptionsItemSelecteditem220. 221.222. // 长点击一条记录触发的时间223. Override224. public boolean onItemLongClickAdapterViewltgt parent View view225. int position long id 226. Student student Student dao.getStudentFromViewview id227. listView.setTagstudent228. return false229. 230.231. // 点击一条记录是触发的事件232. Override233. public void onItemClickAdapterViewltgt parent View view int position234. long id 235. if isDeleteList 236. student dao.getStudentFromViewview id237. // Log.eTAG quotstudentquot dao.getStudentFromViewview id238. Intent intent new Intent239. intent.putExtraquotstudentquot student240. intent.setClassthis ShowStudentActivity.class241. this.startActivityintent242. else 243. CheckBox box CheckBox view.findViewByIdR.id.cb_box244. box.setCheckedbox.isChecked245. list.addid246. deleteButton.setEnabledbox.isChecked247. 248. 249.250. // 自定义一个加载数据库中的全部记录到当前页面的无参方法251. public void load 252. StudentDBHelper studentDBHelper new StudentDBHelper253. StudentListActivity.this254. SQLiteDatabase database studentDBHelper.getWritableDatabase255. cursor database.queryTableContanst.STUDENT_TABLE null null null256. null null TableContanst.StudentColumns.MODIFY_TIME quot descquot257. startManagingCursorcursor258. adapter new SimpleCursorAdapterthis R.layout.student_list_item259. cursor new String TableContanst.StudentColumns.ID260. TableContanst.StudentColumns.NAME261. TableContanst.S.
上一篇:
Android android uid shared MK编译或eclipse编译后签名
下一篇:
用Mozilla免费调试器调试JavaScript