",Toast.LENGTH_LONG).show();}}if (TextUtils.isEmpty(name)) {Toast.makeText(AddStudentsActivity.this, "录入失败
请输入姓名", Toast.LENGTH_SHORT).show();} else if (TextUtils.isEmpty(number)) {Toast.makeText(AddStudentsActivity.this, "录入失败
请输入学号", Toast.LENGTH_SHORT).show();} else if (TextUtils.isEmpty(year)) {Toast.makeText(AddStudentsActivity.this, "录入失败
请输入年龄", Toast.LENGTH_SHORT).show();} else if (TextUtils.isEmpty(sex)) {Toast.makeText(AddStudentsActivity.this, "录入失败
请输入性别", Toast.LENGTH_SHORT).show();} else if (TextUtils.isEmpty(birth)) {Toast.makeText(AddStudentsActivity.this, "录入失败
请输入出生年月", Toast.LENGTH_SHORT).show();} else if (TextUtils.isEmpty(address)) {Toast.makeText(AddStudentsActivity.this, "录入失败
请输入地址", Toast.LENGTH_SHORT).show();} else if (TextUtils.isEmpty(phone)) {Toast.makeText(AddStudentsActivity.this, "录入失败
请输入电话", Toast.LENGTH_SHORT).show();}else if (studentsBeans != null){Toast.makeText(AddStudentsActivity.this, "学号重复", Toast.LENGTH_SHORT).show();}else {System.out.println("是否为空: "+dbStudents);dbStudents.insertStudents(number, name, year, sex, birth, address, phone);showDeleteDialog();}}}/ 修改提示框 /private void showDeleteDialog2() {final AlertDialog.Builder builder = new AlertDialog.Builder(this);builder.setTitle("修改成功提示").setMessage("已成功修改
").setPositiveButton("ok", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {Intent intent=new Intent(AddStudentsActivity.this,MainActivity.class);startActivity(intent);}});builder.create().show();}/ 保存提示框 /private void showDeleteDialog() {final AlertDialog.Builder builder = new AlertDialog.Builder(this);builder.setTitle("录入成功提示").setMessage("已成功录入
\n是否继续?").setPositiveButton("否", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {Intent intent=new Intent(AddStudentsActivity.this,MainActivity.class);startActivity(intent);}}).setNegativeButton("是", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {//将输入框清空EtNumber.setText("");EtName.setText("");EtYear.setText("");EtSex.setText("");EtBirth.setText("");EtAddress.setText("");EtPhone.setText("");}});builder.create().show();}DeleteStudentsActivity类:public class DeleteStudentsActivity extends AppCompatActivity {ListView listView;//列表List<StudentsBean> mData;//数据源Adapter_del adapter;//适配器DbStudents dbStudents;//数据库@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_delete_students);//控件绑定listView = findViewById(R.id.del_lv);dbStudents = new DbStudents(this);mData = new ArrayList<>();System.out.println("长度为:" + mData.size());//设置适配器:加载每一行数据到列表当中adapter = new Adapter_del(this, mData);listView.setAdapter(adapter);loadData();setLVClickListener();}/ listView长按事件/private void setLVClickListener() {listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {@Overridepublic boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {StudentsBean studentsBean = mData.get(position);deleteItem(studentsBean);return false;}});}/ 长按删除 /private void deleteItem(final StudentsBean studentsBean) {final String delNumber = studentsBean.getNumber();AlertDialog.Builder builder = new AlertDialog.Builder(this);builder.setTitle("提示信息").setMessage("您确定要删除这条记录么?").setNegativeButton("取消", null).setPositiveButton("确定", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {DbStudents.deleteItemFromStudentByNumber(delNumber);mData.remove(studentsBean); //实时刷新,从数据源删除adapter.notifyDataSetChanged();}});builder.create().show();}/ 显示学生信息列表 /private void loadData() {List<StudentsBean> list = DbStudents.search();mData.clear();mData.addAll(list);adapter.notifyDataSetChanged();}/ 按钮点击事件 /public void onClick(View v) {switch (v.getId()) {case R.id.iv_back:Intent intent = new Intent(DeleteStudentsActivity.this, MainActivity.class);startActivity(intent);finish();break;}}DataSupper类public class Users extends DataSupport {private String name;private String password;private String email;private String addrs;private int age;private String sex;public Users ( String name, String password) {this.name = name;this.password = password;}public Users ( String name, String password,String email,String addrs,String sex,int age) {this.name = name;this.password = password;this.addrs = addrs;this.email = email;this.age = age;this.sex = sex;}public Users () {}public String get_name () {return name;}public String get_password () { return password; }public String get_email() {return email;}public int get_age(){ return age; }public String get_addrs(){ return addrs; }public String get_sex() { return sex; }public void set_name(String name) {this.name=name;}public void setPassword(String password) {this.password = password;}public void setEmail(String email) {this.email = email;}public void setAge(int age) {this.age = age;}public void setAddrs(String addrs) { this.addrs = addrs; }public void setSex(String sex) { this.sex = sex; }}MainActivity类:public class MainActivity extends AppCompatActivity implements View.OnClickListener{private Button btnAdd,btnSearch,btnMod,btnQuery,btnDelete,btnBack;//按钮:录入、浏览、修改、查询、删除、退出@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//事件绑定btnAdd=findViewById(R.id.add_students);btnSearch=findViewById(R.id.search_students);btnMod=findViewById(R.id.mod_students);btnDelete=findViewById(R.id.delete_students);btnQuery=findViewById(R.id.query_students);btnBack=findViewById(R.id.btn_back);btnBack.setOnClickListener(this);btnQuery.setOnClickListener(this);btnDelete.setOnClickListener(this);btnMod.setOnClickListener(this);btnAdd.setOnClickListener(this);btnSearch.setOnClickListener(this);}/ 按钮点击事件 /@Overridepublic void onClick(View v) {switch (v.getId()){case R.id.add_students:Intent intent1=new Intent(MainActivity.this,AddStudentsActivity.class);startActivity(intent1);break;case R.id.search_students:Intent intent2=new Intent(MainActivity.this,SearchStudentsActivity.class);startActivity(intent2);break;case R.id.mod_students:Intent intent3=new Intent(MainActivity.this,ModStudentsActivity.class);startActivity(intent3);break;case R.id.query_students:Intent intent4=new Intent(MainActivity.this,QueryStudentsActivity.class);startActivity(intent4);break;case R.id.delete_students:Intent intent5=new Intent(MainActivity.this,DeleteStudentsActivity.class);startActivity(intent5);break;case R.id.btn_back:DigLogShow();break;}}/ 退出提示 /private void DigLogShow() {final AlertDialog.Builder builder = new AlertDialog.Builder(this);builder.setTitle("退出提示").setMessage("是否退出?").setPositiveButton("否", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {}}).setNegativeButton("是", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {Intent intent6=new Intent(MainActivity.this,LoginActivity.class);SharedPreferences sharedPreferences = getSharedPreferences("zidong", Context.MODE_PRIVATE);sharedPreferences.edit().clear().apply();Toast.makeText(MainActivity.this,"退出成功
",Toast.LENGTH_LONG).show();startActivity(intent6);}});builder.create().show();}}ModStudentSActivity类:public class ModStudentsActivity extends AppCompatActivity implements View.OnClickListener {ListView UpdateLv;//列表EditText EtNumber;//学号List<StudentsBean> mData;//数据源Adapter_update adapter;//适配器对象DbStudents dbStudents;//数据库@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_mod_students);//绑定事件UpdateLv = findViewById(R.id.update_lv);EtNumber = findViewById(R.id.et_update);mData = new ArrayList<>();adapter = new Adapter_update(this, mData);UpdateLv.setAdapter(adapter);//适配器列表显示dbStudents = new DbStudents(this);}/ 按钮点击事件 /@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.iv_back:Intent intent = new Intent(ModStudentsActivity.this, MainActivity.class);startActivity(intent);finish();break;case R.id.update_iv:String number = EtNumber.getText().toString().trim();StudentsBean studentsBeans = dbStudents.QueryFromStudentByNumber(number);//查询学号是否存在if (TextUtils.isEmpty(number)) {Toast.makeText(this, "请输入学号
", Toast.LENGTH_SHORT).show();} else if (studentsBeans == null) {Toast.makeText(this, "该学号不存在
", Toast.LENGTH_SHORT).show();} else {//开始搜索List<StudentsBean> list = DbStudents.getStudentListByNumber(number);//查询该学号的所有信息mData.clear();//将原来搜索的消息清除mData.addAll(list);adapter.notifyDataSetChanged();//实时更新initData();//修改信息函数break;}}}/ 修改信息点击事件,点击就会进入到修改页面进行修改 /private void initData() {dbStudents= new DbStudents(this); //创建数据库UpdateLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {@Overridepublic void onItemClick(AdapterView<?> parent, View view, int position, long id){StudentsBean studentsBean = mData.get(position);Intent intent = new Intent(ModStudentsActivity.this,AddStudentsActivity.class);intent.putExtra("number", studentsBean.getNumber());intent.putExtra("name", studentsBean.getName());intent.putExtra("year", studentsBean.getYear());intent.putExtra("sex", studentsBean.getSex());intent.putExtra("birth", studentsBean.getBirth());intent.putExtra("address", studentsBean.getAddress());intent.putExtra("phone", studentsBean.getPhone());ModStudentsActivity.this.startActivityForResult(intent, 1);}});}}QueryStudentsActivity类:public class QueryStudentsActivity extends AppCompatActivity implements View.OnClickListener {ListView QueryLv;//列表EditText EtNumber, EtName;//输入框学号和姓名List<StudentsBean> mData;//数据源Adapter adapter;//适配器对象DbStudents dbStudents;//数据库@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_query_students);//绑定所有事件QueryLv = findViewById(R.id.query_lv);EtName = findViewById(R.id.et_name);EtNumber = findViewById(R.id.et_number);mData = new ArrayList<>();adapter = new Adapter(this, mData);QueryLv.setAdapter(adapter);dbStudents=new DbStudents(this);}/ 按钮点击事件 /@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.iv_back:Intent intent = new Intent(QueryStudentsActivity.this, MainActivity.class);//实现页面跳转startActivity(intent);finish();break;case R.id.iv_query:String number = EtNumber.getText().toString().trim();String name = EtName.getText().toString().trim();if (TextUtils.isEmpty(number)) {Toast.makeText(this, "请输入学号
", Toast.LENGTH_SHORT).show();} else if (TextUtils.isEmpty(name)) {Toast.makeText(this, "请输入姓名
", Toast.LENGTH_SHORT).show();} else {//开始搜索List<StudentsBean> list = DbStudents.getStudentListByNumberAndName(number,name);mData.clear();//将原来搜索的消息清除mData.addAll(list);//显示在列表上adapter.notifyDataSetChanged();//实时更新break;}}}SearchStudentsActivity类:public class SearchStudentsActivity extends AppCompatActivity implements View.OnClickListener {ImageButton BtnBack;//退出按钮ListView listView;//列表List<StudentsBean> mData;//数据源Adapter adapter;//适配器DbStudents dbStudents;//数据库@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_search_students);//绑定事件BtnBack = findViewById(R.id.btn_back);BtnBack.setOnClickListener(this);listView = findViewById(R.id.listView);dbStudents = new DbStudents(this);mData = new ArrayList<>();System.out.println("长度为:" + mData.size());//设置适配器:加载每一行数据到列表当中adapter = new Adapter(this, mData);listView.setAdapter(adapter);loadData();//显示学生信息列表函数}/ 显示学生信息列表/private void loadData() {List<StudentsBean> list = DbStudents.search();mData.clear();mData.addAll(list);adapter.notifyDataSetChanged();//实时更新}@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.btn_back:Intent intent = new Intent(SearchStudentsActivity.this, MainActivity.class);startActivity(intent);finish();break;}}}六、数据库设计七、总结这次的课程学习时间比较短,所以完成的项目也不算完美,基础比较差,到做项目的时候问题就全部出来了,所以我还需要继续努力学习
(图片来源网络,侵删)
0 评论