百度上流传最广的版本有所欠缺,并不能直接使用,同时有很多不必要的功能,这是我进行删减、修改、核查后的版本,根据下面的步骤一步步来直接能够运行程序。
目前创新互联公司已为1000多家的企业提供了网站建设、域名、虚拟空间、绵阳服务器托管、企业网站设计、鹰潭网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。
本程序实现的功能是增删改查以及全选
首先是程序提纲
主要部分是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 Listlist; 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 (); 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"?>
student_list_item.xml
<?xml version="1.0" encoding="utf-8"?>
展示单条记录详细信息的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"?>
添加记录的活动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"?>
查找记录的活动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"?>
find_student_list_item.xml
<?xml version="1.0" encoding="utf-8"?>
主界面中跳出的上下文菜单ContextMenu的布局文件menu.xml
menu.xml
然后是一些自定义类的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
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"?>#3F51B5 #303F9F #EE82EE
strings.xml
<?xml version="1.0" encoding="utf-8"?>Hello World, TextStudentManager! 学员管理系统 学员信息修改 男 女 唱歌 跳舞 健身 添加学员信息 请选择
styles.xml
AndroidManifest.xml
再次提醒,所有自定义活动必须手动添加到这个文件中,包括设置主活动也是在此文件中
<?xml version="1.0" encoding="utf-8"?>
最后是效果图片
初始界面
增
添加界面
添加界面中的日历插件
添加后返回主界面
第一次打开的程序,ID是从1开始的,因为我之前有操作过,所以这里ID才不是从1开始的。
单击记录后显示详细信息
长按记录后跳出上下文菜单
删
点击菜单中的删除按钮
删除后
下面看看多条记录的操作
点击主界面的选择按钮
点击全选按钮
全选后删除
改
点击菜单中的修改按钮
查
搜索
搜索结果
以上就是关于我自己修改过的简易学生信息管理系统的全部说明,按照步骤来一步一步做,应该就能直接运。如果有问题的话,顶多就是改一下包的名字和gradle文件中版本号之类的一些简单的问题,具体需要结合电脑的实际情况来修改即可。
欢迎指正。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持创新互联。
当前名称:直接可用的Androidstudio学生信息管理系统
本文链接:http://scyingshan.cn/article/gojcoh.html