Android開發(fā)之學生信息管理系統(tǒng)_第1頁
Android開發(fā)之學生信息管理系統(tǒng)_第2頁
Android開發(fā)之學生信息管理系統(tǒng)_第3頁
Android開發(fā)之學生信息管理系統(tǒng)_第4頁
Android開發(fā)之學生信息管理系統(tǒng)_第5頁
已閱讀5頁,還剩50頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

1、03.04.05.06.07.08.08.19.Android 開發(fā)之學生信息管理系統(tǒng)復制鏈接 今天上課老師讓利用 ListView 和數(shù)據(jù)庫做一個學員信息管理系統(tǒng)。下面我就把自己做的代 碼復制下來,供大家參考。首頁的布局 main.xml 01.<?xml version="1.0" encoding="utf-8"?> 02.<LinearLayout xmlns:android="android:orientation="vertical" and

2、roid:layout_width="fill_parent"android:layout_height="fill_parent"><RelativeLayout android:id="+id/RelativeLayout"android:layout_width="fill_parent" android:layout_height="wrap_content"><Button android:id="+id/bn_search_id"andro

3、id:layout_width="wrap_content" android:layout_height="wrap_content" android:text=" 搜索 " android:gravity="center_vertical" /><Button android:gravity="center" android:text="string/myButton" android:id="+id/btn_add_student" and

4、roid:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_toRightOf="+id/bn_search_id"android:layout_toLeftOf="+id/bn_select" />2.33. 別7

5、.38.39.40.<Button android:gravity="center_vertical"android:text=" 選擇 "android:id="+id/bn_select"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentTop="true" android:layout_alignParentRight=&

6、quot;true"></Button></RelativeLayout><TextViewandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:gravity="center"android:text=" ID 姓 名 年 齡 性II/><ListView android:id="android:id/list"android:layout_wi

7、dth="fill_parent"android:layout_weight="1"android:layout_height="wrap_content"/><LinearLayoutandroid:orientation="horizontal"android:id="+id/showLiner"2.android:visibility="go

8、ne" android:layout_width="fill_parent" android:layout_height="wrap_content"> <Button android:id="+id/bn_delete" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="

9、 刪除 " android:enabled="false"/><Button android:id="+id/bn_selectall" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text=" 全選 "/><Buttonandroid:layout_width="f

10、ill_parent"android:id="+id/bn_canel"63.android:layout_height="wrap_content"64.android:layout_weight="1"65.android:text=" 取消 "66./>67.</LinearLayout>68.69.</LinearLayout>復制代碼創(chuàng)建 listView 中顯示學員信息的 xml 格式 student_list_item.xml01.<?xml versio

11、n="1.0" encoding="utf-8"?>02.<LinearLayout xmlns:android="03.android:orientation="horizontal"04.android:layout_width="fill_parent"05.android:layout_height="wrap_content" >06.<ImageView android:layout_width="fill_parent"07.a

12、ndroid:layout_height="wrap_content"08.android:layout_gravity="center"09.android:layout_weight="1"10.android:background="drawable/icon"/>11.<TextView android:id="+id/tv_stu_id"12.android:layout_width="fill_parent"13.android:layout_grav

13、ity="center"14.android:layout_height="wrap_content"android:layout_weight="1"/>6.<TextView android:id="+id/tv_stu_name" android:layout_width="fill_parent" android:layout_gravity=&qu

14、ot;center" android:layout_height="wrap_content" android:layout_weight="1"/><TextView android:id="+id/tv_stu_age" android:layout_width="fill_parent" android:layout_gravity="center" android:layout_height="wrap_content" android:layo

15、ut_weight="1"/><TextView android:id="+id/tv_stu_sex" android:layout_width="fill_parent" android:layout_gravity="center" android:layout_height="wrap_content" android:layout_weight="1"/><TextView android:id="+id/tv_stu_likes&

16、quot;android:layout_width="fill_parent" android:layout_height="wrap_content"android:layout_gravity="center"android:layout_weight="1"8.android:visibility="gone"/><TextView android:i

17、d="+id/tv_stu_phone" android:layout_width="fill_parent" android:layout_gravity="center" android:layout_height="wrap_content" android:layout_weight="1" android:visibility="gone"/><TextView android:id="+id/tv_stu_traindate" an

18、droid:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_weight="1" android:visibility="gone"/><TextView android:id="+id/tv_stu_modifyDateTime" android:layout_width="fil

19、l_parent" android:layout_gravity="center" android:layout_height="wrap_content" android:layout_weight="1" android:visibility="gone"/><CheckBox59.android:id="+id/cb_box"60.android:layout_width="fill_parent"61.android:layout_height=

20、"wrap_content"62.android:layout_weight="1"63.android:visibility="gone"64.android:checked="false"65.android:focusable="false"66./>67.</LinearLayout>復制代碼創(chuàng)建一個 StudentListActivity 做為主頁顯示學員信息以及進行一些操作。01.package cn.yj3g.student.activity;02.03.impo

21、rt java.util.ArrayList;04.import java.util.Iterator;05.import java.util.List;06.07.import android.app.AlertDialog;08.import android.app.ListActivity;09.import android.content.DialogInterface;10.import android.content.Intent;11.import android.database.Cursor;12.import android.database.sqlite.SQLiteDa

22、tabase;13.import android.os.Bundle;14.import android.util.Log;15.import android.view.ContextMenu;16.import android.view.ContextMenu.ContextMenuInfo;17.import android.view.Menu;18.import android.view.MenuInflater;19.import android.view.MenuItem;20.import android.view.View;21.import android.view.View.

23、OnClickListener;22.import android.widget.AdapterView;23.import android.widget.AdapterView.OnItemClickListener;24.import android.widget.AdapterView.OnItemLongClickListener;25.import android.widget.Button;26.import android.widget.CheckBox;27.import android.widget.LinearLayout;28.import android.widget.

24、ListView;29.import android.widget.RelativeLayout;30.import android.widget.SimpleCursorAdapter;31.import android.widget.Toast;32.import cn.yj3g.student.dao.StudentDao;33.import cn.yj3g.student.db.StudentDBHelper;34.import cn.yj3g.student.entry.Student;3.5

25、5.import cn.yj3g.student.entry.TableContanst;36.37.public class StudentListActivity extends ListActivity implementsOnClickListener, OnItemClickListener, OnItemLongClickListener private static final String TAG = "TestSQLite"private Button addStudent;private Cursor cursor;private Si

26、mpleCursorAdapter adapter;private ListView listView;private List<Long> list;private RelativeLayout relativeLayout;private Button searchButton;private Button selectButton;private Button deleteButton;private Button selectAllButton;private Button canleButton;private LinearLayout layout;private St

27、udentDao dao;private Student student;private Boolean isDeleteList = false;8.Override public void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState); setContentView(R.layout.main);Log.e(TAG , "onCreate");lis

28、t = new ArrayList<Long>();student = new Student();dao = new StudentDao(new StudentDBHelper(this);addStudent = (Button) findViewById(R.id.btn_add_student); searchButton = (Button) findViewById(R.id.bn_search_id); selectButton = (Button) findViewById(R.id.bn_select); deleteButton = (Button) find

29、ViewById(R.id.bn_delete); selectAllButton = (Button) findViewById(R.id.bn_selectall); canleButton = (Button) findViewById(R.id.bn_canel); layout = (LinearLayout) findViewById(R.id.showLiner); relativeLayout=(RelativeLayout) findViewById(R.id.RelativeLayout); listView = getListView();/ 為按鍵設(shè)置監(jiān)聽addStud

30、ent.setOnClickListener(this);searchButton.setOnClickListener(this);selectButton.setOnClickListener(this);00.deleteButton.setOnClickListener(this);canleButton.setOnClickListener(this);selectAllButton.setOnClickListener(this);listView.set

31、OnItemClickListener(this);listView.setOnItemLongClickListener(this);listView.setOnCreateContextMenuListener(this);Overrideprotected void onStart() / 調(diào)用 load() 方法將數(shù)據(jù)庫中的所有記錄顯示在當前頁面super.onStart();load();public void onClick(View v) / 跳轉(zhuǎn)到添加信息的界面if (v = addStudent) startActivity(new Intent(this, AddStude

32、ntActivity.class); else if (v = searchButton) / 跳轉(zhuǎn)到查詢界面startActivity(new Intent(this, StudentSearch.class); else if (v = selectButton) / 跳轉(zhuǎn)到選擇界面isDeleteList = !isDeleteList;if (isDeleteList) checkOrClearAllCheckboxs(true); else showOrHiddenCheckBoxs(false); else if (v = deleteButton) / 刪除數(shù)據(jù)if (list.

33、size() > 0) for (int i = 0; i < list.size(); i+) long id = list.get(i);Log.e(TAG , "delete id=" + id);int count = dao.deleteStudentById(id);dao.closeDB();load();0022. else if (v = canleButton) 123./ 點擊

34、取消,回到初始界面124.load();125.layout.setVisibility(View.GONE);126.isDeleteList = !isDeleteList;127. else if (v = selectAllButton) 128. / 全選,如果當前全選按鈕顯示是全選,則在點擊后變?yōu)槿∠x,如果 當前為取消全選,則在點擊后變?yōu)槿x129.selectAllMethods();130.131.132./ 創(chuàng)建菜單133.Override134.public void onCreateContextMenu(ContextMenu menu, View v,135.Co

35、ntextMenuInfo menuInfo) 136.MenuInflater inflater = getMenuInflater();137.inflater.inflate(R.layout.menu, menu);138.139.140./ 對菜單中的按鈕添加響應時間141.Override142.public boolean onContextItemSelected(MenuItem item) 143.int item_id = item.getItemId();144.student = (Student) listView.getTag();48.

36、64.165.166.Log.v(TAG , "TestSQLite+student+" + listView.getTag() + "");final long student_id = student.getId();Intent intent = new Intent();/ Log.v(TAG , "TestSQLite+id"+student_id);switch (item_id) / 添加case R

37、.id.add:startActivity(new Intent(this, AddStudentActivity.class);break;/ 刪除case R.id.delete:deleteStudentInformation(student_id);break;case R.id.look:/ 查看學生信息/ Log.v(TAG , "TestSQLite+look"+student+"");intent.putExtra("student", student);intent.setClass(this, ShowStuden

38、tActivity.class);this.startActivity(intent);break;case R.id.write:/ 修改學生信息88.intent.putExtra("student", student);intent.setClass(this, AddStudentActivity.class);this.startActivity(intent);break;default:bre

39、ak;return super.onContextItemSelected(item);/ 創(chuàng)建一個按鈕菜單Overridepublic boolean onCreateOptionsMenu(Menu menu) menu.add(1, 1, 1, " 按入學日期排序 ");menu.add(1, 2, 1, " 按姓名進行排序 ");menu.add(1, 5, 1, " 按學號進行排序 ");menu.add(1, 3, 1, "模糊查找 ");menu.add(1, 4, 1, " 退出 &quo

40、t;);return super.onCreateOptionsMenu(menu);/ 對菜單中的按鈕添加響應時間00010.Override public boolean onOptionsItemSelected(MenuItem item) int id = item.getItemId();switch (id) / 排序case 1:cursor = dao.sortByTrainDate();load(cursor);

41、break;/ 排序case 2:cursor = dao.sortByName();load(cursor);break;/ 查找case 3:startActivity(new Intent(this, StudentSearch.class);break;/ 退出case 4:finish();32.break;case 5:cursor = dao.sortByID();load(cursor);break;defau

42、lt:break;return super.onOptionsItemSelected(item);/ 長點擊一條記錄觸發(fā)的時間Overridepublic boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) Student student = (Student) dao.getStudentFromView(view, id);listView.setTag(student);return false;/ 點擊一條記錄是觸發(fā)的事件Override

43、54.public void onItemClick(AdapterView<?> parent, View view, int position,long id) if (!isDeleteList) student = dao.getStudentFromView(view, id);/ Log.e(TAG , "student*" + dao.getStudentFromView(view, id);Intent

44、intent = new Intent();intent.putExtra("student", student);intent.setClass(this, ShowStudentActivity.class);this.startActivity(intent); else CheckBox box = (CheckBox) view.findViewById(R.id.cb_box);box.setChecked(!box.isChecked();list.add(id);deleteButton.setEnabled(box.isChecked();/ 自定義一個加

45、載數(shù)據(jù)庫中的全部記錄到當前頁面的無參方法public void load() StudentDBHelper studentDBHelper = new StudentDBHelper(StudentListActivity.this);SQLiteDatabase database = studentDBHelper.getWritableDatabase();255.256.257.258.259.260.261.262.263.264.265.266.267.268.269.270.271.272.273.274.275.276.cursor = database.query(Table

46、Contanst.STUDENT_TABLE, null, null, null,null, null, TableContanst.StudentColumns.MODIFY_TIME + " desc");startManagingCursor(cursor);adapter = new SimpleCursorAdapter(this, R.layout.student_list_item,cursor, new String TableContanst.StudentColumns.ID,TableContanst.StudentColumns.NAME,Table

47、Contanst.StudentColumns.AGE,TableContanst.StudentColumns.SEX,TableContanst.StudentColumns.LIKES, TableContanst.StudentColumns.PHONE_NUMBER, TableContanst.StudentColumns.TRAIN_DA TE , new int R.id.tv_stu_id, R.id.tv_stu_name, R.id.tv_stu_age, R.id.tv_stu_sex, R.id.tv_stu_likes, R.id.tv_stu_phone, R.i

48、d.tv_stu_traindate );listView.setAdapter(adapter);/ 自定義一個加載數(shù)據(jù)庫中的全部記錄到當前頁面的有參方法public void load(Cursor cursor) adapter = new SimpleCursorAdapter(this, R.layout.student_list_item,cursor, new String TableContanst.StudentColumns.ID,TableContanst.StudentColumns.NAME,277.278.279.280.281.282.283.284.285.28

49、6.287.288.289.290.291.292.293.294.295.296.297.298.TableContanst.StudentColumns.AGE,TableContanst.StudentColumns.SEX,TableContanst.StudentColumns.LIKES,TableContanst.StudentColumns.PHONE_NUMBER,TableContanst.StudentColumns.TRAIN_DATE , new int R.id.tv_stu_id, R.id.tv_stu_name, R.id.tv_stu_age,R.id.tv

50、_stu_sex, R.id.tv_stu_likes, R.id.tv_stu_phone,R.id.tv_stu_traindate );listView.setAdapter(adapter);/ 全選或者取消全選private void checkOrClearAllCheckboxs(boolean b) int childCount = listView.getChildCount();/ Log.e(TAG , "list child size=" + childCount);for (int i = 0; i < childCount; i+) Vie

51、w view = listView.getChildAt(i);if (view != null) CheckBox box = (CheckBox) view.findViewById(R.id.cb_box);box.setChecked(!b);299.300.301.302.303.304.305.306.307.308.309.310.311.312.313.314.315.316.317.318.319.320.showOrHiddenCheckBoxs(true);/ 顯示或者隱藏自定義菜單private void showOrHiddenCheckBoxs(boolean b)

52、 int childCount = listView.getChildCount();/ Log.e(TAG , "list child size=" + childCount);for (int i = 0; i < childCount; i+) View view = listView.getChildAt(i);if (view != null) CheckBox box = (CheckBox) view.findViewById(R.id.cb_box); int visible = b ? View.VISIBLE : View.GONE;box.set

53、Visibility(visible);layout.setVisibility(visible);deleteButton.setEnabled(false);/ 自定義一個利用對話框形式進行數(shù)據(jù)的刪除private void deleteStudentInformation(final long delete_id) 321.322.323.324.325.326.327.328.329.330.331.332.333.334.335.336.337.338.339.340.341.342./ 利用對話框的形式刪除數(shù)據(jù)AlertDialog.Builder builder = new Al

54、ertDialog.Builder(this);builder.setTitle(" 學員信息刪除 ").setMessage(”確定刪除所選記錄?").setCancelable(false).setPositiveButton(" 確定 ", new DialogInterface.OnClickListener() public void onClick(DialogInterface dialog, int id) int raws = dao.deleteStudentById(delete_id); layout.setVisibi

55、lity(View.GONE);isDeleteList = !isDeleteList;load();if (raws > 0) Toast.makeText(StudentListActivity.this, " 刪除成功 !",Toast.LENGTH_LONG).show(); elseToast.makeText(StudentListActivity.this, " 刪除失敗 !", Toast.LENGTH_LONG).show();).setNegativeButton(" 取消 ", new DialogInt

56、erface.OnClickListener() public void onClick(DialogInterface dialog, int id) dialog.cancel();343.344.);345.AlertDialog alert = builder.create();346.alert.show();347. 348.349. /點擊全選事件時所觸發(fā)的響應350.private void selectAllMethods() 351./ 全選,如果當前全選按鈕顯示是全選,則在點擊后變?yōu)槿∠x,如果當前為取消全選,則在點擊后變?yōu)槿x352.if (selectAllButt

57、on.getText().toString().equals(" 全選 ") 353.int childCount = listView.getChildCount();354.for (int i = 0; i < childCount; i+) 355.View view = listView.getChildAt(i);356.if (view != null) 357.CheckBox box = (CheckBox) view.findViewById(R.id.cb_box);358.box.setChecked(true);359.deleteButto

58、n.setEnabled(true);360.selectAllButton.setText(" 取消全選 ");361.362.363. else if (selectAllButton.getText().toString().equals(" 取消全選 ") 364.checkOrClearAllCheckboxs(true);365.deleteButton.setEnabled(false);366.selectAllButton.setText(" 全選 ");367. 368. 369.復制代碼menu.xml 文件01

59、.<menu xmlns:android="02. <group android:checkableBehavior="single">03.<item android:id="+id/delete" android:title=" 刪除學員信息 " />04.<item android:id="+id/look" android:title=" 詳細信息 " />05.<item android:id="+id/add" android:title=" 添加學員信息 " />06.<item android:id="+id/write" android:title=" 修改學員信息 " />0

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論