您好,登錄后才能下訂單哦!
百度上流傳最廣的版本有所欠缺,并不能直接使用,同時有很多不必要的功能,這是我進行刪減、修改、核查后的版本,根據下面的步驟一步步來直接能夠運行程序。
本程序實現的功能是增刪改查以及全選
首先是程序提綱
主要部分是java文件和xml文件。
activity放在java文件里面,xml文件就是布局文件,用來規定界面的顯示格式。
類定義的Java文件
StudentDao
StudnetDBHelper
Student
TableContanst
其他文件
string .xml
color.xml
styles.xml
AndroidManifest.xml(自定義的活動需要手動添加到此文件中)
下面看看我的文件目錄
值得注意的是,menu.xml不是放在layout目錄下,而是放在menu目錄下。
然后依次介紹各個activity的代碼
主界面是StudentListActivity
代碼如下
java文件:StudentListActivity
package com.example.asus.student; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import android.app.AlertDialog; import android.app.ListActivity; import android.content.DialogInterface; import android.content.Intent; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.util.Log; import android.view.ContextMenu; import android.view.ContextMenu.ContextMenuInfo; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.AdapterView.OnItemLongClickListener; import android.widget.Button; import android.widget.CheckBox; import android.widget.LinearLayout; import android.widget.ListView; import android.widget.RelativeLayout; import android.widget.SimpleCursorAdapter; import android.widget.Toast; import StudentDBHelper.StudentDBHelper; import Student.Student; //import AddStudentActivity; import TableContanst.TableContanst; public class StudentListActivity extends ListActivity implements OnClickListener, OnItemClickListener, OnItemLongClickListener { private static final String TAG = "TestSQLite"; private Button addStudent; private Cursor cursor; private SimpleCursorAdapter 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 StudentDao dao; private Student student; private Boolean isDeleteList = false; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Log.e(TAG, "onCreate"); list = 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) findViewById(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(); // 為按鍵設置監聽 addStudent.setOnClickListener(this); searchButton.setOnClickListener(this); selectButton.setOnClickListener(this); deleteButton.setOnClickListener(this); canleButton.setOnClickListener(this); selectAllButton.setOnClickListener(this); listView.setOnItemClickListener(this); listView.setOnItemLongClickListener(this); listView.setOnCreateContextMenuListener(this); } // 調用load()方法將數據庫中的所有記錄顯示在當前頁面 @Override protected void onStart() { super.onStart(); load(); } public void onClick(View v) { // 跳轉到添加信息的界面 if (v == addStudent) { startActivity(new Intent(StudentListActivity.this, AddStudentActivity.class)); } else if (v == searchButton) { // 跳轉到查詢界面 startActivity(new Intent(this, StudentSearch.class)); } else if (v == selectButton) { // 跳轉到選擇界面 isDeleteList = !isDeleteList; if (isDeleteList) { checkOrClearAllCheckboxs(true); } else { showOrHiddenCheckBoxs(false); } } else if (v == deleteButton) { // 刪除數據 if (list.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(); } } else if (v == canleButton) { // 點擊取消,回到初始界面 load(); layout.setVisibility(View.GONE); isDeleteList = !isDeleteList; } else if (v == selectAllButton) { // 全選,如果當前全選按鈕顯示是全選,則在點擊后變為取消全選,如果當前為取消全選,則在點擊后變為全選 selectAllMethods(); } } // 創建菜單 public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { MenuInflater inflater = new MenuInflater(this); //getMenuInflater(); inflater.inflate(R.menu.menu, menu); } // 對菜單中的按鈕添加響應時間 @Override public boolean onContextItemSelected(MenuItem item) { int item_id = item.getItemId(); student = (Student) listView.getTag(); 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.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, ShowStudentActivity.class); this.startActivity(intent); break; case R.id.write: // 修改學生信息 intent.putExtra("student", student); intent.setClass(this, AddStudentActivity.class); this.startActivity(intent); break; default: break; } return super.onContextItemSelected(item); } @Override public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { Student student = (Student) dao.getStudentFromView(view, id); listView.setTag(student); registerForContextMenu(listView); return false; } // 點擊一條記錄是觸發的事件 @Override 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 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()); } } // 自定義一個加載數據庫中的全部記錄到當前頁面的無參方法 public void load() { StudentDBHelper studentDBHelper = new StudentDBHelper( StudentListActivity.this); SQLiteDatabase database = studentDBHelper.getWritableDatabase(); cursor = database.query(TableContanst.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, 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_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++) { View view = listView.getChildAt(i); if (view != null) { CheckBox box = (CheckBox) view.findViewById(R.id.cb_box); box.setChecked(!b); } } showOrHiddenCheckBoxs(true); } // 顯示或者隱藏自定義菜單 private void showOrHiddenCheckBoxs(boolean b) { 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.setVisibility(visible); layout.setVisibility(visible); deleteButton.setEnabled(false); } } } // 自定義一個利用對話框形式進行數據的刪除 private void deleteStudentInformation(final long delete_id) { // 利用對話框的形式刪除數據 AlertDialog.Builder builder = new AlertDialog.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.setVisibility(View.GONE); isDeleteList = !isDeleteList; load(); if (raws > 0) { Toast.makeText(StudentListActivity.this, "刪除成功!", Toast.LENGTH_LONG).show(); } else Toast.makeText(StudentListActivity.this, "刪除失敗!", Toast.LENGTH_LONG).show(); } }) .setNegativeButton("取消", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.cancel(); } }); AlertDialog alert = builder.create(); alert.show(); } // 點擊全選事件時所觸發的響應 private void selectAllMethods() { // 全選,如果當前全選按鈕顯示是全選,則在點擊后變為取消全選,如果當前為取消全選,則在點擊后變為全選 if (selectAllButton.getText().toString().equals("全選")) { int childCount = listView.getChildCount(); for (int i = 0; i < childCount; i++) { View view = listView.getChildAt(i); if (view != null) { CheckBox box = (CheckBox) view.findViewById(R.id.cb_box); box.setChecked(true); deleteButton.setEnabled(true); selectAllButton.setText("取消全選"); } } } else if (selectAllButton.getText().toString().equals("取消全選")) { checkOrClearAllCheckboxs(true); deleteButton.setEnabled(false); selectAllButton.setText("全選"); } } }
布局文件:main.xml和student_list_item.xml
代碼如下
main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android: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" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="搜索" android:gravity="center_vertical" /> <Button android:gravity="center" android:text="添加學員信息" android:id="@+id/btn_add_student" android: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" /> <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="true"></Button> </RelativeLayout> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:text=" ID 姓 名 年 齡 性 別 " /> <ListView android:id="@android:id/list" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" /> <LinearLayout android:orientation="horizontal" android:id="@+id/showLiner" android:visibility="gone" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center_horizontal"> <Button android:id="@+id/bn_delete" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="刪除" 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="全選" /> <Button android:id="@+id/bn_canel" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="取消" /> </LinearLayout> </LinearLayout>
student_list_item.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" > <ImageView android:layout_width="fill_parent" android:layout_height="130px" android:layout_gravity="center" android:layout_weight="1" android:background="@drawable/icon" /> <TextView android:id="@+id/tv_stu_id" android:layout_width="fill_parent" android:layout_gravity="center" android:layout_height="wrap_content" android:layout_weight="1"/> <TextView android:id="@+id/tv_stu_name" android:layout_width="fill_parent" android:layout_gravity="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:layout_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" 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_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" android: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="fill_parent" android:layout_gravity="center" android:layout_height="wrap_content" android:layout_weight="1" android:visibility="gone"/> <CheckBox android:id="@+id/cb_box" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:visibility="gone" android:checked="false" android:focusable="false"/> </LinearLayout>
展示單條記錄詳細信息的ShowStudentActivity
代碼如下
java文件:ShowStudentActivity
package com.example.asus.student; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.TextView; import Student.Student; import TableContanst.TableContanst; public class ShowStudentActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.student_info); Intent intent = getIntent(); Student student = (Student) intent.getSerializableExtra(TableContanst.STUDENT_TABLE); ((TextView)findViewById(R.id.tv_info_id)).setText(student.getId()+""); ((TextView)findViewById(R.id.tv_info_name)).setText(student.getName()); ((TextView)findViewById(R.id.tv_info_age)).setText(student.getAge()+""); ((TextView)findViewById(R.id.tv_info_sex)).setText(student.getSex()); ((TextView)findViewById(R.id.tv_info_likes)).setText(student.getLike()); ((TextView)findViewById(R.id.tv_info_train_date)).setText(student.getTrainDate()); ((TextView)findViewById(R.id.tv_info_phone)).setText(student.getPhoneNumber()); } public void goBack(View view) { finish(); } }
布局文件:student_info.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="5dip" > <TextView android:id="@+id/id2_text_id" android:layout_width="80dip" android:layout_height="40dip" android:layout_marginRight="5dip" android:layout_marginTop="5dip" android:layout_marginBottom="5dip" android:textSize="16sp" android:gravity="left|center_vertical" android:text="學員ID:" /> <TextView android:id="@+id/tv_info_id" android:layout_width="fill_parent" android:layout_height="40dip" android:layout_toRightOf="@id/id2_text_id" android:layout_alignParentRight="true" android:layout_alignTop="@id/id2_text_id" android:gravity="left|center_vertical"/> <TextView android:id="@+id/name2_text_id" android:layout_width="80dip" android:layout_height="40dip" android:layout_marginRight="5dip" android:layout_marginTop="5dip" android:layout_marginBottom="5dip" android:layout_below="@id/id2_text_id" android:layout_alignLeft="@id/id2_text_id" android:textSize="16sp" android:gravity="left|center_vertical" android:text="姓名:" /> <TextView android:id="@+id/tv_info_name" android:layout_width="fill_parent" android:layout_height="40dip" android:layout_toRightOf="@id/name2_text_id" android:layout_alignParentRight="true" android:layout_alignTop="@id/name2_text_id" android:gravity="left|center_vertical" /> <TextView android:id="@+id/age2_text_id" android:layout_width="80dip" android:layout_height="40dip" android:gravity="left|center_vertical" android:layout_marginRight="5dip" android:layout_below="@id/name2_text_id" android:layout_marginBottom="5dip" android:textSize="16sp" android:text="年齡:" /> <TextView android:id="@+id/tv_info_age" android:layout_width="fill_parent" android:layout_height="40dip" android:layout_toRightOf="@id/age2_text_id" android:layout_alignParentRight="true" android:layout_alignTop="@id/age2_text_id" android:gravity="left|center_vertical" /> <TextView android:id="@+id/sex2_text_id" android:layout_width="80dip" android:layout_height="40dip" android:gravity="left|center_vertical" android:layout_below="@id/age2_text_id" android:layout_alignLeft="@id/age2_text_id" android:layout_marginRight="5dip" android:layout_marginBottom="5dip" android:text="性別:" android:textSize="16sp" /> <TextView android:id="@+id/tv_info_sex" android:layout_width="fill_parent" android:layout_height="40dip" android:layout_toRightOf="@id/sex2_text_id" android:layout_alignParentRight="true" android:layout_alignTop="@id/sex2_text_id" android:gravity="left|center_vertical" /> <TextView android:id="@+id/like2_text_id" android:layout_width="80dip" android:layout_height="40dip" android:gravity="left|center_vertical" android:layout_below="@id/sex2_text_id" android:layout_alignLeft="@id/sex2_text_id" android:layout_marginRight="5dip" android:layout_marginBottom="5dip" android:text="愛好:" android:textSize="16sp" /> <TextView android:layout_height="40dip" android:id="@+id/tv_info_likes" android:layout_width="wrap_content" android:layout_toRightOf="@id/like2_text_id" android:layout_below="@id/sex2_text_id" android:layout_marginRight="52dip" android:gravity="left|center_vertical"/> <TextView android:id="@+id/contact2_text_id" android:layout_width="80dip" android:layout_height="40dip" android:gravity="center_vertical|left" android:layout_marginRight="5dip" android:layout_below="@id/like2_text_id" android:layout_marginBottom="5dip" android:textSize="16sp" android:text="聯系電話:" /> <TextView android:id="@+id/tv_info_phone" android:layout_width="fill_parent" android:layout_height="40dip" android:layout_toRightOf="@id/contact2_text_id" android:layout_alignParentRight="true" android:layout_alignTop="@id/contact2_text_id" android:gravity="center_vertical|left" /> <TextView android:id="@+id/train2_time_text_id" android:layout_width="80dip" android:layout_height="40dip" android:gravity="center_vertical|left" android:layout_marginRight="5dip" android:layout_below="@id/contact2_text_id" android:layout_marginBottom="5dip" android:textSize="16sp" android:text="入學日期" /> <TextView android:id="@+id/tv_info_train_date" android:layout_width="fill_parent" android:layout_height="40dip" android:layout_toRightOf="@id/train2_time_text_id" android:layout_alignParentRight="true" android:layout_alignTop="@id/train2_time_text_id" android:gravity="center_vertical|left" /> <Button android:id="@+id/back_to_list_id" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="返回列表界面" android:layout_below="@id/train2_time_text_id" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:onClick="goBack"> </Button> </RelativeLayout>
添加記錄的活動AddStudentActivity
代碼如下
java文件:AddStudenActivity
package com.example.asus.student; import java.io.Serializable; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.HashSet; import android.app.Activity; import android.app.DatePickerDialog; import android.app.Dialog; import android.content.ContentValues; import android.content.Intent; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.CompoundButton.OnCheckedChangeListener; import android.widget.DatePicker; import android.widget.EditText; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.TextView; import android.widget.Toast; import StudentDBHelper.StudentDBHelper; import Student.Student; import TableContanst.TableContanst; public class AddStudentActivity extends Activity implements OnClickListener { private static final String TAG = "AddStudentActivity"; private final static int DATE_DIALOG = 1; private static final int DATE_PICKER_ID = 1; private TextView idText; private EditText nameText; private EditText ageText; private EditText phoneText; private EditText dataText; private RadioGroup group; private RadioButton button1; private RadioButton button2; private CheckBox box1; private CheckBox box2; private CheckBox box3; private Button restoreButton; private String sex; private Button resetButton; private Long student_id; private StudentDao dao; private boolean isAdd = true; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.add_student); idText = (TextView) findViewById(R.id.tv_stu_id); nameText = (EditText) findViewById(R.id.et_name); ageText = (EditText) findViewById(R.id.et_age); button1 = (RadioButton) findViewById(R.id.rb_sex_female); button2 = (RadioButton) findViewById(R.id.rb_sex_male); phoneText = (EditText) findViewById(R.id.et_phone); dataText = (EditText) findViewById(R.id.et_traindate); group = (RadioGroup) findViewById(R.id.rg_sex); box1 = (CheckBox) findViewById(R.id.box1); box2 = (CheckBox) findViewById(R.id.box2); box3 = (CheckBox) findViewById(R.id.box3); restoreButton = (Button) findViewById(R.id.btn_save); resetButton = (Button) findViewById(R.id.btn_clear); dao = new StudentDao(new StudentDBHelper(this)); // 設置監聽 78 restoreButton.setOnClickListener(this); resetButton.setOnClickListener(this); dataText.setOnClickListener(this); checkIsAddStudent(); } // 檢查此時Activity是否用于添加學員信息 private void checkIsAddStudent() { Intent intent = getIntent(); Serializable serial = intent.getSerializableExtra(TableContanst.STUDENT_TABLE); if (serial == null) { isAdd = true; dataText.setText(getCurrentDate()); } else { isAdd = false; Student s = (Student) serial; showEditUI(s); } } //顯示學員信息更新的UI104 private void showEditUI(Student student) { // 先將Student攜帶的數據還原到student的每一個屬性中去 student_id = student.getId(); String name = student.getName(); int age = student.getAge(); String phone = student.getPhoneNumber(); String data = student.getTrainDate(); String like = student.getLike(); String sex = student.getSex(); if (sex.toString().equals("男")) { button2.setChecked(true); } else if (sex.toString().equals("女")) { button1.setChecked(true); } if (like != null && !"".equals(like)) { if (box1.getText().toString().indexOf(like) >= 0) { box1.setChecked(true); } if (box2.getText().toString().indexOf(like) >= 0) { box2.setChecked(true); } if (box3.getText().toString().indexOf(like) >= 0) { box3.setChecked(true); } } // 還原數據 idText.setText(student_id + ""); nameText.setText(name + ""); ageText.setText(age + ""); phoneText.setText(phone + ""); dataText.setText(data + ""); setTitle("學員信息更新"); restoreButton.setText("更新"); } public void onClick(View v) { // 收集數據 if (v == restoreButton) { if (!checkUIInput()) {// 界面輸入驗證 return; } Student student = getStudentFromUI(); if (isAdd) { long id = dao.addStudent(student); dao.closeDB(); if (id > 0) { Toast.makeText(this, "保存成功, ID=" + id,Toast.LENGTH_SHORT).show(); finish(); } else { Toast.makeText(this, "保存失敗,請重新輸入!", Toast.LENGTH_SHORT).show(); } } else if (!isAdd) { long id = dao.addStudent(student); dao.closeDB(); if (id > 0) { Toast.makeText(this, "更新成功",Toast.LENGTH_SHORT).show(); finish(); } else { Toast.makeText(this, "更新失敗,請重新輸入!",Toast.LENGTH_SHORT).show(); } } } else if (v == resetButton) { clearUIData(); } else if (v == dataText) { showDialog(DATE_PICKER_ID); } } // 清空界面的數據176 private void clearUIData() { nameText.setText(""); ageText.setText(""); phoneText.setText(""); dataText.setText(""); box1.setChecked(false); box2.setChecked(false); group.clearCheck(); } // 收集界面輸入的數據,并將封裝成Student對象 private Student getStudentFromUI() { String name = nameText.getText().toString(); int age = Integer.parseInt(ageText.getText().toString()); String sex = ((RadioButton) findViewById(group .getCheckedRadioButtonId())).getText().toString(); String likes = ""; if (box1.isChecked()) { // basketball, football football likes += box1.getText(); } if (box2.isChecked()) { if (likes.equals("")) { likes += box2.getText(); } else { likes += "," + box2.getText(); } if (likes.equals("")) { likes += box3.getText(); } else { likes += "," + box3.getText(); } } String trainDate = dataText.getText().toString(); String phoneNumber = phoneText.getText().toString(); String modifyDateTime = getCurrentDateTime(); Student s=new Student(name, age, sex, likes, phoneNumber, trainDate, modifyDateTime); if (!isAdd) { s.setId(Integer.parseInt(idText.getText().toString())); dao.deleteStudentById(student_id); } return s; } // * 得到當前的日期時間 private String getCurrentDateTime() { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); return format.format(new Date()); } // * 得到當前的日期 private String getCurrentDate() { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); return format.format(new Date()); } //驗證用戶是否按要求輸入了數據 private boolean checkUIInput() { // name, age, sex String name = nameText.getText().toString(); String age = ageText.getText().toString(); int id = group.getCheckedRadioButtonId(); String message = null; View invadView = null; if (name.trim().length() == 0) { message = "請輸入姓名!"; invadView = nameText; } else if (age.trim().length() == 0) { message = "請輸入年齡!"; invadView = ageText; } else if (id == -1) { message = "請選擇性別!"; } if (message != null) { Toast.makeText(this, message, Toast.LENGTH_SHORT).show(); if (invadView != null) invadView.requestFocus(); return false; } return true; } //時間的監聽與事件 private DatePickerDialog.OnDateSetListener onDateSetListener = new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { dataText.setText(year + "-" + (monthOfYear + 1) + "-" + dayOfMonth); } }; @Override protected Dialog onCreateDialog(int id) { switch (id) { case DATE_PICKER_ID: return new DatePickerDialog(this, onDateSetListener, 2011, 8, 14); } return null; } }
布局文件:add_student.xml
<?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:fillViewport="true" android:scrollbarStyle="outsideInset" > <RelativeLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="5dip" > <TextView android:id="@+id/tv_stu_text_id" android:layout_width="80dip" android:layout_height="40dip" android:gravity="center_vertical|right" android:layout_marginRight="5dip" android:layout_marginTop="5dip" android:layout_marginBottom="5dip" android:textSize="16sp" android:text="學員ID:" /> <TextView android:id="@+id/tv_stu_id" android:layout_width="fill_parent" android:layout_height="40dip" android:text="未分配ID" android:layout_toRightOf="@id/tv_stu_text_id" android:layout_alignParentRight="true" android:layout_alignTop="@id/tv_stu_text_id" android:gravity="center" android:background="#ffffff" android:textColor="#000000" android:textSize="16sp" /> <TextView android:id="@+id/tv_name_text" android:layout_width="80dip" android:layout_height="40dip" android:gravity="center_vertical|right" android:layout_marginRight="5dip" android:layout_below="@id/tv_stu_text_id" android:layout_alignLeft="@id/tv_stu_text_id" android:layout_marginBottom="5dip" android:textSize="16sp" android:text="姓名:" /> <EditText android:id="@+id/et_name" android:layout_width="fill_parent" android:layout_height="40dip" android:layout_toRightOf="@id/tv_name_text" android:layout_alignParentRight="true" android:layout_alignTop="@id/tv_name_text" android:hint="請輸入姓名,如liukenken" android:inputType="textPersonName" android:paddingLeft="20dip"/> <TextView android:id="@+id/tv_age_text" android:layout_width="80dip" android:layout_height="40dip" android:gravity="center_vertical|right" android:layout_marginRight="5dip" android:layout_below="@id/tv_name_text" android:layout_marginBottom="5dip" android:textSize="16sp" android:text="年齡:" /> <EditText android:id="@+id/et_age" android:layout_width="fill_parent" android:layout_height="40dip" android:layout_toRightOf="@id/tv_age_text" android:layout_alignParentRight="true" android:layout_alignTop="@id/tv_age_text" android:hint="請輸入年齡" android:paddingLeft="20dip" android:maxLength="3" android:inputType="number" /> <TextView android:id="@+id/tv_sex_text" android:layout_width="80dip" android:layout_height="40dip" android:gravity="center_vertical|right" android:layout_below="@id/tv_age_text" android:layout_alignLeft="@id/tv_age_text" android:layout_marginRight="5dip" android:layout_marginBottom="5dip" android:text="性別:" android:textSize="16sp" /> <RadioGroup android:id="@+id/rg_sex" android:layout_width="fill_parent" android:layout_height="40dip" android:orientation="horizontal" android:layout_toRightOf="@id/tv_sex_text" android:layout_alignParentRight="true" android:layout_alignTop="@id/tv_sex_text" > <RadioButton android:id="@+id/rb_sex_male" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="男" android:textSize="16sp" /> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="女" android:id="@+id/rb_sex_female" android:layout_weight="1" android:textSize="16sp"> </RadioButton> </RadioGroup> <TextView android:id="@+id/tv_likes_text" android:layout_width="80dip" android:layout_height="40dip" android:gravity="center_vertical|right" android:layout_below="@id/rg_sex" android:layout_alignLeft="@id/tv_sex_text" android:layout_marginRight="5dip" android:layout_marginBottom="5dip" android:text="愛好:" android:textSize="16sp" /> <CheckBox android:id="@+id/box1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@id/tv_likes_text" android:layout_below="@+id/rg_sex" android:text="@string/box1" ></CheckBox> <CheckBox android:id="@+id/box2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@+id/box1" android:layout_below="@+id/rg_sex" android:text="@string/box2"> </CheckBox> <CheckBox android:id="@+id/box3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@+id/box2" android:layout_below="@+id/rg_sex" android:text="@string/box3" > </CheckBox> <TextView android:id="@+id/tv_phone_text" android:layout_width="80dip" android:layout_height="40dip" android:gravity="center_vertical|right" android:layout_marginRight="5dip" android:layout_below="@id/tv_likes_text" android:layout_marginBottom="5dip" android:textSize="16sp" android:text="聯系電話:" /> <EditText android:id="@+id/et_phone" android:layout_width="fill_parent" android:layout_height="40dip" android:layout_toRightOf="@id/tv_phone_text" android:layout_alignParentRight="true" android:layout_alignTop="@id/tv_phone_text" android:hint="請輸入手機號" android:paddingLeft="20dip" android:inputType="phone" android:maxLength="11" /> <TextView android:id="@+id/tv_traindate_text" android:layout_width="80dip" android:layout_height="40dip" android:gravity="center_vertical|right" android:layout_marginRight="5dip" android:layout_below="@id/tv_phone_text" android:layout_marginBottom="5dip" android:textSize="16sp" android:text="入學日期" /> <EditText android:id="@+id/et_traindate" android:layout_width="fill_parent" android:layout_height="40dip" android:layout_toRightOf="@id/tv_traindate_text" android:layout_alignParentRight="true" android:layout_alignTop="@id/tv_traindate_text" android:hint="點擊選擇日期" android:inputType="date" android:paddingLeft="20dip" android:focusable="false" /> <Button android:id="@+id/btn_save" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="保存" android:layout_below="@id/tv_traindate_text" android:layout_alignRight="@id/rg_sex"> </Button> <Button android:id="@+id/btn_clear" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="重置" android:layout_below="@id/tv_traindate_text" android:layout_toLeftOf="@id/btn_save" android:layout_marginRight="10dip"> </Button> </RelativeLayout> </ScrollView>
查找記錄的活動StudentSearch。
代碼如下
java文件:StudentSearch
package com.example.asus.student; import StudentDBHelper.StudentDBHelper; import TableContanst.TableContanst; import android.app.Activity; import android.content.Intent; import android.database.Cursor; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.LinearLayout; import android.widget.ListView; import android.widget.SimpleCursorAdapter; import android.widget.Toast; public class StudentSearch extends Activity implements OnClickListener { private EditText nameText; private Button button; private Button reButton; private Cursor cursor; private SimpleCursorAdapter adapter; private ListView listView; private StudentDao dao; private Button returnButton; private LinearLayout layout; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.search); nameText = (EditText) findViewById(R.id.et_srarch); layout=(LinearLayout) findViewById(R.id.linersearch); button = (Button) findViewById(R.id.bn_sure_search); reButton = (Button) findViewById(R.id.bn_return); listView = (ListView) findViewById(R.id.searchListView); returnButton = (Button) findViewById(R.id.return_id); dao = new StudentDao(new StudentDBHelper(this)); reButton.setOnClickListener(this); returnButton.setOnClickListener(this); button.setOnClickListener(this); } @Override public void onClick(View v) { if (v == button) { reButton.setVisibility(View.GONE); button.setVisibility(View.GONE); nameText.setVisibility(View.GONE); layout.setVisibility(View.VISIBLE); String name = nameText.getText().toString(); cursor = dao.findStudent(name); if (!cursor.moveToFirst()) { Toast.makeText(this, "沒有所查學員信息!", Toast.LENGTH_SHORT).show(); } else //如果有所查詢的信息,則將查詢結果顯示出來 adapter = new SimpleCursorAdapter(this, R.layout.find_student_list_item, cursor, new String[] { TableContanst.StudentColumns.ID, TableContanst.StudentColumns.NAME, 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_stu_sex, R.id.tv_stu_likes, R.id.tv_stu_phone, R.id.tv_stu_traindate }); listView.setAdapter(adapter); }else if(v==reButton|v==returnButton){ finish(); } } }
布局文件:search.xml和find_studetn_list_item.xml
代碼如下
search.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <EditText android:id="@+id/et_srarch" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="請輸入學員姓名" android:inputType="textPersonName" /> <Button android:id="@+id/bn_sure_search" android:gravity="center" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="確定" /> <Button android:id="@+id/bn_return" android:gravity="center" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="返回" /> <LinearLayout android:id="@+id/linersearch" android:orientation="vertical" android:visibility="gone" android:layout_width="fill_parent" android:layout_height="wrap_content"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:text="ID 姓 名 年 齡 性 別 愛 好 電 話 日 期" /> <ListView android:id="@+id/searchListView" android:layout_weight="1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="right"/> <Button android:id="@+id/return_id" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="返回" /> </LinearLayout> </LinearLayout>
find_student_list_item.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" > <TextView android:id="@+id/tv_stu_id" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" /> <TextView android:id="@+id/tv_stu_name" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" /> <TextView android:id="@+id/tv_stu_age" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" /> <TextView android:id="@+id/tv_stu_sex" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" /> <TextView android:id="@+id/tv_stu_likes" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" /> <TextView android:id="@+id/tv_stu_phone" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" /> <TextView android:id="@+id/tv_stu_traindate" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" /> </LinearLayout>
主界面中跳出的上下文菜單ContextMenu的布局文件menu.xml
menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="40dip" android:layout_width="80dip"> <group android:checkableBehavior="single"> <item android:id="@+id/delete" android:title="刪除學員信息" /> <item android:id="@+id/look" android:title="詳細信息" /> <item android:id="@+id/write" android:title="修改學員信息" /> </group> </menu>
然后是一些自定義類的java文件
StudentDao類
java文件:StudentDao
package com.example.asus.student; import StudentDBHelper.StudentDBHelper; import TableContanst.TableContanst; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.content.ContentValues; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.view.View; import android.widget.SimpleCursorAdapter; import android.widget.TextView; import Student.Student; public class StudentDao { private StudentDBHelper dbHelper; private Cursor cursor; public StudentDao(StudentDBHelper dbHelper) { this.dbHelper = dbHelper; } // 添加一個Student對象數據到數據庫表 public long addStudent(Student s) { ContentValues values = new ContentValues(); values.put(TableContanst.StudentColumns.NAME, s.getName()); values.put(TableContanst.StudentColumns.AGE, s.getAge()); values.put(TableContanst.StudentColumns.SEX, s.getSex()); values.put(TableContanst.StudentColumns.LIKES, s.getLike()); values.put(TableContanst.StudentColumns.PHONE_NUMBER, s.getPhoneNumber()); values.put(TableContanst.StudentColumns.TRAIN_DATE, s.getTrainDate()); values.put(TableContanst.StudentColumns.MODIFY_TIME, s.getModifyDateTime()); return dbHelper.getWritableDatabase().insert(TableContanst.STUDENT_TABLE, null, values); } // 刪除一個id所對應的數據庫表student的記錄 public int deleteStudentById(long id) { return dbHelper.getWritableDatabase().delete(TableContanst.STUDENT_TABLE, TableContanst.StudentColumns.ID + "=?", new String[] { id + "" }); } // 更新一個id所對應數據庫表student的記錄 public int updateStudent(Student s) { ContentValues values = new ContentValues(); values.put(TableContanst.StudentColumns.NAME, s.getName()); values.put(TableContanst.StudentColumns.AGE, s.getAge()); values.put(TableContanst.StudentColumns.SEX, s.getSex()); values.put(TableContanst.StudentColumns.LIKES, s.getLike()); values.put(TableContanst.StudentColumns.PHONE_NUMBER, s.getPhoneNumber()); values.put(TableContanst.StudentColumns.TRAIN_DATE, s.getTrainDate()); values.put(TableContanst.StudentColumns.MODIFY_TIME, s.getModifyDateTime()); return dbHelper.getWritableDatabase().update(TableContanst.STUDENT_TABLE, values, TableContanst.StudentColumns.ID + "=?", new String[] { s.getId() + "" }); } // 查詢所有的記錄 public List<Map<String,Object>> getAllStudents() { //modify_time desc List<Map<String, Object>> data = new ArrayList<Map<String,Object>>(); Cursor cursor = dbHelper.getWritableDatabase().query(TableContanst.STUDENT_TABLE, null, null, null, null, null, TableContanst.StudentColumns.MODIFY_TIME+" desc"); while(cursor.moveToNext()) { Map<String, Object> map = new HashMap<String, Object>(8); long id = cursor.getInt(cursor.getColumnIndex(TableContanst.StudentColumns.ID)); map.put(TableContanst.StudentColumns.ID, id); String name = cursor.getString(cursor.getColumnIndex(TableContanst.StudentColumns.NAME)); map.put(TableContanst.StudentColumns.NAME, name); int age = cursor.getInt(cursor.getColumnIndex(TableContanst.StudentColumns.AGE)); map.put(TableContanst.StudentColumns.AGE, age); String sex = cursor.getString(cursor.getColumnIndex(TableContanst.StudentColumns.SEX)); map.put(TableContanst.StudentColumns.SEX, sex); String likes = cursor.getString(cursor.getColumnIndex(TableContanst.StudentColumns.LIKES)); map.put(TableContanst.StudentColumns.LIKES, likes); String phone_number = cursor.getString(cursor.getColumnIndex(TableContanst.StudentColumns.PHONE_NUMBER)); map.put(TableContanst.StudentColumns.PHONE_NUMBER, phone_number); String train_date = cursor.getString(cursor.getColumnIndex(TableContanst.StudentColumns.TRAIN_DATE)); map.put(TableContanst.StudentColumns.TRAIN_DATE, train_date); String modify_time = cursor.getString(cursor.getColumnIndex(TableContanst.StudentColumns.MODIFY_TIME)); map.put(TableContanst.StudentColumns.MODIFY_TIME, modify_time); data.add(map); } return data; } //模糊查詢一條記錄 public Cursor findStudent(String name){ Cursor cursor = dbHelper.getWritableDatabase().query(TableContanst.STUDENT_TABLE, null, "name like ?", new String[] { "%" + name + "%" }, null, null, null,null); return cursor; } //按姓名進行排序 public Cursor sortByName(){ Cursor cursor = dbHelper.getWritableDatabase().query(TableContanst.STUDENT_TABLE, null,null, null, null, null,TableContanst.StudentColumns.NAME); return cursor; } //按入學日期進行排序 public Cursor sortByTrainDate(){ Cursor cursor = dbHelper.getWritableDatabase().query(TableContanst.STUDENT_TABLE, null,null, null, null, null,TableContanst.StudentColumns.TRAIN_DATE); return cursor; } //按學號進行排序 public Cursor sortByID(){ Cursor cursor = dbHelper.getWritableDatabase().query(TableContanst.STUDENT_TABLE, null,null, null, null, null,TableContanst.StudentColumns.ID); return cursor; } public void closeDB() { dbHelper.close(); } //自定義的方法通過View和Id得到一個student對象 public Student getStudentFromView(View view, long id) { TextView nameView = (TextView) view.findViewById(R.id.tv_stu_name); TextView ageView = (TextView) view.findViewById(R.id.tv_stu_age); TextView sexView = (TextView) view.findViewById(R.id.tv_stu_sex); TextView likeView = (TextView) view.findViewById(R.id.tv_stu_likes); TextView phoneView = (TextView) view.findViewById(R.id.tv_stu_phone); TextView dataView = (TextView) view.findViewById(R.id.tv_stu_traindate); String name = nameView.getText().toString(); int age = Integer.parseInt(ageView.getText().toString()); String sex = sexView.getText().toString(); String like = likeView.getText().toString(); String phone = phoneView.getText().toString(); String data = dataView.getText().toString(); Student student = new Student(id, name, age, sex, like, phone, data,null); return student; } }
StudentDBHelper類
java文件:StudentDBHelper
package StudentDBHelper; import TableContanst.TableContanst; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase.CursorFactory; import android.database.sqlite.SQLiteOpenHelper; import android.util.Log; public class StudentDBHelper extends SQLiteOpenHelper { private static final String TAG = "StudentDBHelper"; public static final String DB_NAME = "student_manager.db"; public static final int VERSION = 1; //構造方法 public StudentDBHelper(Context context, String name, CursorFactory factory, int version) { super(context, name, factory, version); } public StudentDBHelper(Context context) { this(context, DB_NAME, null, VERSION); } //創建數據庫 @Override public void onCreate(SQLiteDatabase db) { Log.v(TAG, "onCreate"); db.execSQL("create table " + TableContanst.STUDENT_TABLE + "(_id Integer primary key AUTOINCREMENT," + "name char,age integer, sex char, likes char, phone_number char,train_date date, " + "modify_time DATETIME)"); } //更新數據庫 @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { Log.v(TAG, "onUpgrade"); } }
Student類
java文件:Student
package Student; import java.io.Serializable; import android.view.View; import android.widget.TextView; public class Student implements Serializable{ private long id; private String name; private int age; private String sex; private String like; private String phoneNumber; private String trainDate; private String modifyDateTime; public Student() { super(); } public Student(long id, String name, int age, String sex, String like, String phoneNumber, String trainDate, String modifyDateTime) { super(); this.id = id; this.name = name; this.age = age; this.sex = sex; this.like = like; this.phoneNumber = phoneNumber; this.trainDate = trainDate; this.modifyDateTime = modifyDateTime; } public Student(String name, int age, String sex, String like, String phoneNumber, String trainDate, String modifyDateTime) { super(); this.name = name; this.age = age; this.sex = sex; this.like = like; this.phoneNumber = phoneNumber; this.trainDate = trainDate; this.modifyDateTime = modifyDateTime; } public long getId() { return id; } public void setId(long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getLike() { return like; } public void setLike(String like) { this.like = like; } public String getPhoneNumber() { return phoneNumber; } public void setPhoneNumber(String phoneNumber) { this.phoneNumber = phoneNumber; } public String getTrainDate() { return trainDate; } public void setTrainDate(String trainDate) { this.trainDate = trainDate; } public String getModifyDateTime() { return modifyDateTime; } public void setModifyDateTime(String modifyDateTime) { this.modifyDateTime = modifyDateTime; } }
TableContanst類
java文件:TableContanst
package TableContanst; public final class TableContanst { public static final String STUDENT_TABLE = "student"; public static final class StudentColumns { public static final String ID = "_id"; public static final String NAME = "name"; public static final String AGE = "age"; public static final String SEX = "sex"; public static final String LIKES = "likes"; public static final String PHONE_NUMBER = "phone_number"; public static final String TRAIN_DATE = "train_date"; public static final String MODIFY_TIME = "modify_time"; } }
其他文件
color.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <color name="colorPrimary">#3F51B5</color> <color name="colorPrimaryDark">#303F9F</color> <color name="colorAccent">#EE82EE</color> </resources>
strings.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello World, TextStudentManager!</string> <string name="app_name">學員管理系統</string> <string name="information_write">學員信息修改</string> <string name="button1">男</string> <string name="button2">女</string> <string name="box1">唱歌</string> <string name="box2">跳舞</string> <string name="box3">健身</string> <string name="myButton">添加學員信息</string> <string name="spinner">請選擇</string> </resources>
styles.xml
<resources> <!-- Base application theme. --> <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> </style> </resources>
AndroidManifest.xml
再次提醒,所有自定義活動必須手動添加到這個文件中,包括設置主活動也是在此文件中
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.asus.student"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".StudentListActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".AddStudentActivity"> </activity> <activity android:name=".ShowStudentActivity"> </activity> <activity android:name=".StudentSearch"> </activity> </application> </manifest>
最后是效果圖片
初始界面
增
添加界面
添加界面中的日歷插件
添加后返回主界面
第一次打開的程序,ID是從1開始的,因為我之前有操作過,所以這里ID才不是從1開始的。
單擊記錄后顯示詳細信息
長按記錄后跳出上下文菜單
刪
點擊菜單中的刪除按鈕
刪除后
下面看看多條記錄的操作
點擊主界面的選擇按鈕
點擊全選按鈕
全選后刪除
改
點擊菜單中的修改按鈕
查
搜索
搜索結果
以上就是關于我自己修改過的簡易學生信息管理系統的全部說明,按照步驟來一步一步做,應該就能直接運。如果有問題的話,頂多就是改一下包的名字和gradle文件中版本號之類的一些簡單的問題,具體需要結合電腦的實際情況來修改即可。
歡迎指正。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。