1、4、具体实现1.主界面:主界面显示所有功能,每个按钮点击后,跳转进入相应功能核心代码:public class Main extends Activity SQLiteDatabase db; Button btn_search; Button btn_modify; Button btn_add; Button btn_delete; Button btn_quit; Button btn_show; Override protected void onCreate(Bundle savedInstanceState) requestWindowFeature(Window.FEATURE_
2、NO_TITLE); getWindow().setFlags(, ; super.onCreate(savedInstanceState); setContentView(; /打开数据库,若不存在,则创建 db = SQLiteDatabase.openOrCreateDatabase(this.getFilesDir().toString()+/Student.db3, null); btn_search = (Button) findViewById(; btn_modify = (Button) findViewById(; btn_add = (Button) findViewBy
3、Id(; btn_delete = (Button) findViewById(; btn_quit = (Button) findViewById(; btn_show = (Button) findViewById(; try Cursor cursor = db.rawQuery(select * from student cursor.close(); catch(SQLiteException e) db.execSQL(create table student + ( name TEXT, NO TEXT Primary Key, sex TEXT, profession TEXT
4、, age TEXT); /显示所有数据按钮的功能实现 btn_show.setOnClickListener(new OnClickListener() public void onClick(View source) /获取指针 Cursor cursor = db.rawQuery( /判断数据库是否不存在任何数据 if(cursor.moveToFirst() = false) Toast.makeText(Main.this, 不存在记录, Toast.LENGTH_SHORT).show(); else List p = new ArrayList re_name = new Ar
5、rayList info = new ArrayList /保存搜索出的所有数据 for(cursor.moveToFirst() ; !cursor.isAfterLast() ; cursor.moveToNext() int nameColume = cursor.getColumnIndex(name int NOColume = cursor.getColumnIndex(NO int proColume = cursor.getColumnIndex(profession int sexColume = cursor.getColumnIndex(sex int ageColume
6、 = cursor.getColumnIndex(age Student student = new Student(); student.name = 姓名:+cursor.getString(nameColume); student.NO = 学号:+cursor.getString(NOColume); student.sex = 性别:+cursor.getString(sexColume); student.profession = 专业:+cursor.getString(proColume); student.age = 年龄:+cursor.getString(ageColum
7、e); p.add(student); String temp = student.MakeString(); info.add(temp); String newname = cursor.getString(nameColume); re_name.add(newname); /对保存的数据进行封装 String Cur_name = new Stringre_name.size(); Cur_name = re_name.toArray(Cur_name); String Cur_info = new Stringinfo.size(); Cur_info = info.toArray(
8、Cur_info); Bundle bundle = new Bundle(); bundle.putStringArray(, Cur_name); Student data = new Student(); data.info = Cur_info; /将封装的数据传递给结果界面的activity Intent intent = new Intent(Main.this,SearchResult.class); intent.putExtras(bundle); intent.putExtra(data, data); startActivity(intent); cursor.close
9、(); );/为剩下的按钮绑定监听器实现跳转功能 btn_search.setOnClickListener(new OnClickListener() Intent intent = new Intent(Main.this,Search.class); startActivity(intent); btn_modify.setOnClickListener(new OnClickListener() Intent intent = new Intent(Main.this,Modify.class); btn_add.setOnClickListener(new OnClickListen
10、er() Intent intent = new Intent(Main.this,Add.class); btn_delete.setOnClickListener(new OnClickListener() Intent intent = new Intent(Main.this,Delete.class); btn_quit.setOnClickListener(new OnClickListener() db.close(); finish(); 2.数据显示界面:按姓名排列,点击条目展开具体信息public class SearchResult extends Activity Su
11、ppressLint(RtlHardcoded public void onCreate(Bundle savedInstanceState) /获取传送来的数据 final Intent intent = getIntent(); BaseExpandableListAdapter adapter = new BaseExpandableListAdapter()/提取数据 Bundle bundle = intent.getExtras(); Student mem_data = (Student) getIntent().getExtras().get( String people =
12、(String) bundle.getSerializable( String data = mem_data.info; public Object getChild(int groupPosition,int childPosition) return datagroupPositionchildPosition; public long getChildId(int groupPosition,int childPosition) return childPosition; public int getChildrenCount(int groupPosition) return dat
13、agroupPosition.length; /设定每个子选项每行的显示方式 private TextView getTextView() AbsListView.LayoutParams lp = new AbsListView.LayoutParams( , ; TextView textView = new TextView(SearchResult.this); textView.setLayoutParams(lp); textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT); textView.setPadding(36
14、, 0, 0, 0); textView.setTextSize(20); return textView; /设定每个子选项显示内容 public View getChildView(int groupPosition , int childPosition,boolean isLastChild,View convertView,ViewGroup Parent) TextView textView = getTextView(); textView.setText( +getChild(groupPosition,childPosition).toString(); public Obj
15、ect getGroup(int groupPosition) return peoplegroupPosition; public int getGroupCount() return people.length; public long getGroupId(int groupPosition) return groupPosition; /设定每个组选项显示内容 public View getGroupView(int groupPosition, boolean isExpanded ,View convertView , ViewGroup parnet) LinearLayout
16、ll = new LinearLayout(SearchResult.this); ll.setOrientation(0);+getGroup(groupPosition).toString(); ll.addView(textView); return ll; ; ExpandableListView expandListView = (ExpandableListView) findViewById(; expandListView.setAdapter(adapter);3.增添数据界面:根据文本框输入内容进行数据的插入,且具有完整性和重复性的判断,插入成功失败均会产生提示public
17、 class Add extends Activity Button btn_Accept; Button btn_Cancle; TextView ET_name; TextView ET_NO; TextView ET_Pro; TextView ET_Age; RadioGroup rg; String radio_sex = 男; db = SQLiteDatabase.openDatabase(this.getFilesDir().toString()+, null,SQLiteDatabase.OPEN_READWRITE); btn_Accept = (Button) findV
18、iewById(; btn_Cancle = (Button) findViewById(; ET_name = (TextView) findViewById(; ET_NO = (TextView) findViewById(; ET_Pro = (TextView) findViewById(; ET_Age = (TextView) findViewById(; rg = (RadioGroup) findViewById(; rg.setOnCheckedChangeListener(new OnCheckedChangeListener() public void onChecke
19、dChanged(RadioGroup group, int CheckedId) radio_sex = CheckedId = ? :女 /提交操作 btn_Accept.setOnClickListener(new OnClickListener() String name = ET_name.getText().toString(); String NO = ET_NO.getText().toString(); String sex = radio_sex; String pro = ET_Pro.getText().toString(); String age = ET_Age.g
20、etText().toString(); /规范性与完整性判断 try /插入数据 db.execSQL(insert into student values( ?, ?,new String name, NO, sex, pro, age); catch(SQLiteException e) Toast.makeText(Add.this, 插入数据失败,请检查数据规范性与学号的唯一性 return; Toast.makeText(Add.this, 成功插入一条数据:+n+name+NO+sex+pro+age, Toast.LENGTH_SHORT).show(); btn_Cancle
21、.setOnClickListener(new OnClickListener()4.修改数据界面:查找界面:对文本框内输入的数据进行精确查找,成功后转入修改界面修改界面:文本框内默认显示之前的数据,修改完成点击确定以文本框内的信息对数据进行更新查找:btn_Accept.setOnClickListener(new OnClickListener() String name = ET_Modify_Name.getText().toString(); String NO = ET_Modify_No.getText().toString();select * from student where + name=?and NO=? , new String name, NO); /判断查找结果是否为空 Toast.makeText(Modify.this, 记录不存在 String mem_name = null; String mem_No = null; String mem_profession = null; String mem_sex = null; String mem_age = null; /保存所有数据
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1