1、Android学生信息管理系统APP 一、 需求分析 为了方便进行对学生数据库操作,本app可在android设备上进行对学生信息数据库信息管理功效,具体功效以下: 1.对数据库中全部学生姓名进行显示,对各个条目进行点击可展开具体信息 2. 查询数据:查询数据是依据姓名和学号两个条件进行查询,二者满足任一条件则进行模糊查询,两个条件同时满足则进行正确查询,查询结果界面和功效一中相同,以姓名排列,点击展开全部信息 3. 增加数据:在数据库中增添条目,包含姓名(字符串),学号(数字,主键),性别(单选框),年纪(数字),专业(字符串)。每个条目全部有误输入设定,且主键可检验反
2、复性,全部数据可检验完整性,若插入成功则会显示一条消息提醒成功,若失败则会提醒检验主键反复或数据不完整 4. 修改数据:依据姓名学号进行正确查找,查找成功后转入修改界面,为了预防漏填和便捷修改界面会默认填充之前数据(除学号),修改完成即可更新,一样会检验数据完整性 5. 删除数据:依据姓名学号进行正确查找,查找成功则会进行删除,并显示一条删除成功提醒,若失败,也会进行提醒 二、 概念结构设计 ER图: 三、 逻辑结构设计 学生: 姓名(字符串) 学号(数字,主码) 性别(单选框) 年纪(数字) 专业(字符串) create table stud
3、ent ( name TEXT, NO TEXT Primary Key, sex TEXT, profession TEXT, age TEXT ) 四、 具体实现 1.主界面: 主界面显示全部功效,每个按钮点击后,跳转进入对应功效 关键代码: public class Main extends Activity { SQLiteDatabase db; Button btn_search; Button btn_modify; Button btn_add; Button btn_delete; Button
4、 btn_quit; Button btn_show; @Override protected void onCreate(Bundle savedInstanceState) { requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); super.onCreate(savedInstanceSta
5、te); setContentView(R.layout.layout_main); //打开数据库,若不存在,则创建 db = SQLiteDatabase.openOrCreateDatabase(this.getFilesDir().toString()+"/Student.db3", null); btn_search = (Button) findViewById(R.id.btn_search); btn_modify = (Button) findViewById(R.id.btn_modify); btn_add = (Button)
6、 findViewById(R.id.btn_add); btn_delete = (Button) findViewById(R.id.btn_delete); btn_quit = (Button) findViewById(R.id.btn_quit); btn_show = (Button) findViewById(R.id.Btn_show); try { Cursor cursor = db.rawQuery("select * from student", null); cursor.close(); }
7、 catch(SQLiteException e) { db.execSQL("create table student" + "(" + " name TEXT," + " NO TEXT Primary Key," + " sex TEXT," + " profession TEXT," + " age TEXT" + ")"); } //显示全部数据按钮功效实现 btn_show.setOnClickListener(new OnClickListener()
8、
{
public void onClick(View source) {
//获取指针
Cursor cursor = db.rawQuery("select * from student", null);
//判定数据库是否不存在任何数据
if(cursor.moveToFirst() == false)
{
Toast.makeText(Main.this, "不存在统计", Toast.LENGTH_SHORT).show();
}
else
{
List 9、t> p = new ArrayList 10、name");
int NOColume = cursor.getColumnIndex("NO");
int proColume = cursor.getColumnIndex("profession");
int sexColume = cursor.getColumnIndex("sex");
int ageColume = cursor.getColumnIndex("age");
Student student = new Student();
student.name = "姓名:" 11、cursor.getString(nameColume);
student.NO = "学号:"+cursor.getString(NOColume);
student.sex = "性别:"+cursor.getString(sexColume);
student.profession = "专业:"+cursor.getString(proColume);
student.age = "年纪:"+cursor.getString(ageColume);
p.add(student);
String[] t 12、emp = student.MakeString();
info.add(temp);
String newname = cursor.getString(nameColume);
re_name.add(newname);
}
//对保留数据进行封装
String[] Cur_name = new String[re_name.size()];
Cur_name = re_name.toArray(Cur_name);
String[][] Cur_info = n 13、ew String[info.size()][];
Cur_info = info.toArray(Cur_info);
Bundle bundle = new Bundle();
bundle.putStringArray("name", Cur_name);
Student data = new Student();
data.info = Cur_info;
//将封装数据传输给结果界面activity
Intent intent = new Intent(Main.this,SearchR 14、esult.class);
intent.putExtras(bundle);
intent.putExtra("data", data);
startActivity(intent);
cursor.close();
}
}
});
//为剩下按钮绑定监听器实现跳转功效
btn_search.setOnClickListener(new OnClickListener()
{
public void onClick(View source) {
Intent inte 15、nt = new Intent(Main.this,Search.class);
startActivity(intent);
}
});
btn_modify.setOnClickListener(new OnClickListener()
{
public void onClick(View source) {
Intent intent = new Intent(Main.this,Modify.class);
startActivity(intent);
}
});
16、 btn_add.setOnClickListener(new OnClickListener()
{
public void onClick(View source) {
Intent intent = new Intent(Main.this,Add.class);
startActivity(intent);
}
});
btn_delete.setOnClickListener(new OnClickListener()
{
public void onClick(View source) 17、{
Intent intent = new Intent(Main.this,Delete.class);
startActivity(intent);
}
});
btn_quit.setOnClickListener(new OnClickListener()
{
public void onClick(View source) {
db.close();
finish();
}
});
}
}
2. 数据显示界面:
按姓名排列,点击条目展开具体信息
18、
关键代码:
public class SearchResult extends Activity
{
@SuppressLint("RtlHardcoded")
public void onCreate(Bundle savedInstanceState)
{
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSC 19、REEN);
//获取传送来数据
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_result);
final Intent intent = getIntent();
BaseExpandableListAdapter adapter = new BaseExpandableListAdapter()
{
//提取数据
Bundle bundle = intent.getExtras();
Student mem_data = (Student 20、) getIntent().getExtras().get("data");
String[] people = (String[]) bundle.getSerializable("name");
String[][] data = mem_data.info;
public Object getChild(int groupPosition,int childPosition)
{
return data[groupPosition][childPosition];
}
public long getChildI 21、d(int groupPosition,int childPosition)
{
return childPosition;
}
public int getChildrenCount(int groupPosition)
{
return data[groupPosition].length;
}
//设定每个子选项每行显示方法
private TextView getTextView()
{
AbsListView.LayoutParams lp = new AbsListView.La 22、youtParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
TextView textView = new TextView(SearchResult.this);
textView.setLayoutParams(lp);
textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
textView.setPadding(36, 0, 0, 0);
tex 23、tView.setTextSize(20);
return textView;
}
//设定每个子选项显示内容
public View getChildView(int groupPosition , int childPosition,boolean isLastChild,View convertView,ViewGroup Parent)
{
TextView textView = getTextView();
textView.setText(" "+getChild(groupPosition,childPositio 24、n).toString());
return textView;
}
public Object getGroup(int groupPosition)
{
return people[groupPosition];
}
public int getGroupCount()
{
return people.length;
}
public long getGroupId(int groupPosition)
{
return groupPosition; 25、
}
//设定每个组选项显示内容
public View getGroupView(int groupPosition, boolean isExpanded ,View convertView , ViewGroup parnet)
{
LinearLayout ll = new LinearLayout(SearchResult.this);
ll.setOrientation(0);
TextView textView = getTextView();
textView.setText(" "+getGroup( 26、groupPosition).toString());
ll.addView(textView);
return ll;
}
};
ExpandableListView expandListView = (ExpandableListView) findViewById(R.id.list);
expandListView.setAdapter(adapter);
}
}
3. 增添数据界面:
依据文本框输入内容进行数据插入,且含有完整性和反复性判定,插入成功失败均会产生提醒
关键代码 27、
public class Add extends Activity {
SQLiteDatabase db;
Button btn_Accept;
Button btn_Cancle;
TextView ET_name;
TextView ET_NO;
TextView ET_Pro;
TextView ET_Age;
RadioGroup rg;
String radio_sex = "男";
@Override
protected void onCreate(Bundle savedInstanceState) 28、{
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_add);
db = SQLiteDatabase.openDatabase(this.getFile 29、sDir().toString()+"/Student.db3", null,SQLiteDatabase.OPEN_READWRITE);
btn_Accept = (Button) findViewById(R.id.btn_Accept);
btn_Cancle = (Button) findViewById(R.id.btn_Cancle);
ET_name = (TextView) findViewById(R.id.ET_Add_name);
ET_NO = (TextView) findViewById(R.id.ET_Add_NO); 30、
ET_Pro = (TextView) findViewById(R.id.ET_Add_Pro);
ET_Age = (TextView) findViewById(R.id.ET_Add_Age);
rg = (RadioGroup) findViewById(R.id.rg);
rg.setOnCheckedChangeListener(new OnCheckedChangeListener(){
public void onCheckedChanged(RadioGroup group, int CheckedId){
31、 radio_sex = CheckedId == R.id.rad_male ? "男" : "女";
}
});
//提交操作
btn_Accept.setOnClickListener(new OnClickListener()
{
public void onClick(View source) {
String name = ET_name.getText().toString();
String NO = ET_NO.getText().toString();
String sex = 32、radio_sex;
String pro = ET_Pro.getText().toString();
String age = ET_Age.getText().toString();
//规范性和完整性判定
try
{
//插入数据
db.execSQL("insert into student values( ?, ?, ?, ?, ?)",new String[] { name, NO, sex, pro, age});
}
//规范性和完整性判定
catch(SQLiteExc 33、eption e)
{
Toast.makeText(Add.this, "插入数据失败,请检验数据规范性和学号唯一性", Toast.LENGTH_SHORT).show();
return;
}
Toast.makeText(Add.this, "成功插入一条数据:"+"\n"+name+"\n"+NO+"\n"+sex+"\n"+pro+"\n"+age, Toast.LENGTH_SHORT).show();
}
});
btn_Cancle.setOnClickListener( 34、new OnClickListener()
{
public void onClick(View source) {
db.close();
finish();
}
});
}
}
4. 修改数据界面:
查找界面:
对文本框内输入数据进行正确查找,成功后转入修改界面
修改界面:
文本框内默认显示之前数据,修改完成点击确定以文本框内信息对数据进行更新
关键代码:
查找:
btn_Accept.setOnClickListener(new OnCl 35、ickListener()
{
public void onClick(View source) {
String name = ET_Modify_Name.getText().toString();
String NO = ET_Modify_No.getText().toString();
Cursor cursor = db.rawQuery("select * from student where "
+ "name=?"
+ "and NO=?"
, new Str 36、ing[] { name, NO});
//判定查找结果是否为空
if(cursor.moveToFirst() == false)
{
Toast.makeText(Modify.this, "统计不存在", Toast.LENGTH_SHORT).show();
}
else
{
String mem_name = null;
String mem_No = null;
String mem_profession = null;
String mem_sex = null 37、
String mem_age = null;
//保留全部数据
for(cursor.moveToFirst() ; !cursor.isAfterLast() ; cursor.moveToNext())
{
int nameColume = cursor.getColumnIndex("name");
int NoColume = cursor.getColumnIndex("NO");
int proColume = cursor.getColumnIndex("profession");
38、 int sexColume = cursor.getColumnIndex("sex");
int ageColume = cursor.getColumnIndex("age");
mem_name = cursor.getString(nameColume);
mem_No = cursor.getString(NoColume);
mem_profession = cursor.getString(proColume);
mem_sex = cursor.getString(sexColume 39、);
mem_age = cursor.getString(ageColume);
}
//封装全部数据
Bundle bundle = new Bundle();
bundle.putString("name", mem_name);
bundle.putString("No", mem_No);
bundle.putString("profession", mem_profession);
bundle.putString("sex", mem_sex);
bundle.putSt 40、ring("age", mem_age);
//传输数据
Intent intent = new Intent(Modify.this,ModifyResult.class);
intent.putExtras(bundle);
startActivity(intent);
cursor.close();
}
}
});
btn_Cancle.setOnClickListener(new OnClickListener()
{
public void on 41、Click(View source) {
// TODO Auto-generated method stub
db.close();
finish();
}
});
修改:
public class ModifyResult extends Activity
{
SQLiteDatabase db;
Button btn_accept;
Button btn_cancle;
TextView TextView_ModifyResult_No;
EditText ET_ModifyResult 42、Name;
EditText ET_ModifyResult_pro;
EditText ET_ModifyResult_age;
RadioGroup rg;
String radio_sex;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_modifyresult);
//获取数据
final Intent intent = getInten 43、t();
Bundle bundle = intent.getExtras();
db = SQLiteDatabase.openDatabase(this.getFilesDir().toString()+"/Student.db3", null,SQLiteDatabase.OPEN_READWRITE);
btn_accept = (Button) findViewById(R.id.btn_modifyresult_accept);
btn_cancle = (Button) findViewById(R.id.btn_modifyresult 44、cancle);
TextView_ModifyResult_No = (TextView) findViewById(R.id.TextView_ModifyResult_No);
ET_ModifyResult_Name = (EditText) findViewById(R.id.ET_ModifyResult_Name);
ET_ModifyResult_pro = (EditText) findViewById(R.id.ET_ModifyResult_pro);
ET_ModifyResult_age = (EditText) findVie 45、wById(R.id.ET_ModifyResult_age);
rg = (RadioGroup) findViewById(R.id.modify_rg);
//设定默认数据
String name = bundle.getString("name");
final String No = bundle.getString("No");
String pro = bundle.getString("profession");
String age = bundle.getString("age");
radio_sex = bundle.get 46、String("sex");
TextView_ModifyResult_No.setText(No);
ET_ModifyResult_Name.setText(name);
ET_ModifyResult_pro.setText(pro);
ET_ModifyResult_age.setText(age);
rg.setOnCheckedChangeListener(new OnCheckedChangeListener(){
public void onCheckedChanged(RadioGroup group, int Chec 47、kedId){
radio_sex = CheckedId == R.id.rad_male ? "男" : "女";
}
});
btn_accept.setOnClickListener(new OnClickListener()
{
public void onClick(View source) {
String new_name = ET_ModifyResult_Name.getText().toString();
String new_profession = ET_ModifyResult_pro.ge 48、tText().toString();
String new_age = ET_ModifyResult_age.getText().toString();
String new_sex = radio_sex;
//更新数据
try
{
db.execSQL("UPDATE student "
+ "SET name=? ,NO=? ,sex=? ,profession=? ,age=? "
+ "WHERE NO=?",
new String[] {new_name , No , 49、new_sex , new_profession , new_age , No});
}
catch(SQLiteException e)
{
Toast.makeText(ModifyResult.this, "更新数据失败", Toast.LENGTH_SHORT).show();
return;
}
Toast.makeText(ModifyResult.this, "更新数据成功", Toast.LENGTH_SHORT).show();
finish();
}
});
50、
btn_cancle.setOnClickListener(new OnClickListener()
{
public void onClick(View source) {
db.close();
finish();
}
});
}
}
5. 查找数据界面:
对文本框内数据进行模糊查询,查询成功则跳转只查询结果界面,查询失败则产生对应提醒
关键代码:
public class Search extends Activity {
SQLiteDataba






