在 Android : ListView 学生管理 中已经使用 ListView 控件实现简易学生管理功能,现在尝试采用 SQLite 数据库本地保存学生信息,并完善查询功能。
使用工具
- android studio (ver. 3.5.1)
- android(sdk 29)
- java(ver.1.8.0)
- gradle(ver. 5.4.1)
功能实现
1.创建学生实体类。
public class StuInfo implements Serializable {
private String name;
private String major;
private String age;
private String sex;
private String kecheng;
private String academy;
private String date;
private int id;
······
2.设计一个的列表来显示学生信息。
3.创建 MySQLiteAccess 类访问数据库。
public class MySQLiteAccess extends SQLiteOpenHelper {
/**
* @param context:上下文
* @param version:版本号
*/
public MySQLiteAccess(Context context, int version) {
super(context, "stu4.db", null, version);
}
/**
* 数据库文件创建成功后调用
* SQL操作的语句
* @param db 数据库 stuAdmin.db
* 列:
* id(int类型主键:学号)
* name(text)
* sex(text)
* age(int)
*/
@Override
public void onCreate(SQLiteDatabase db) {
System.out.println("数据库创建");
db.execSQL("create table students(n"
"id integer primary key autoincrement,n"
"name text,n"
"sex text,n"
"age integer,n"
"academy text,n"
"major textn,"
"date textn"
");");
db.execSQL("create table courseTable(n"
"stuno text ,n"
"course text n"
");"
);
}
······
3.创建 DBop 类对数据库中数据进行操作。
public class DBop {
private MySQLiteAccess mySQLiteAccess;
private SQLiteDatabase database;
public void test(Context context) {
mySQLiteAccess = new MySQLiteAccess(context, 3);
database = mySQLiteAccess.getReadableDatabase();
}
public void insert(StuInfo s) {
int age = Integer.parseInt(s.getAge());
database.execSQL("insert into students values(?,?,?,?,?,?,?"
")", new Object[]{null, s.getName(), s.getSex(), age, s.getAcademy(), s.getMajor(), s.getDate()});
System.out.println("插入数据成功");
}
public void delete(int id) {
Integer I = new Integer(id);
// String n=new String(name);
database.execSQL("delete from students where id=?", new Object[]{I});
}
public List<StuInfo> searchByName(String key) {
List<StuInfo> listByName = new ArrayList<StuInfo>();
key = "%" key "%";
Cursor cursor = database.rawQuery("select * from students where name like?", new String[]{key});
while (cursor.moveToNext()) {
// int id=cursor.getInt(cursor.getColumnIndex("id"));
String name = cursor.getString(cursor.getColumnIndex("name"));
String sex = cursor.getString(cursor.getColumnIndex("sex"));
String age = cursor.getString(cursor.getColumnIndex("age"));
listByName.add(new StuInfo(name, sex, age));
}
return listByName;
}
······
4.创建 StudentAdapter
public class StudentAdapter extends BaseAdapter implements View.OnClickListener {
······
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View stuView = View.inflate(stuContext, R.layout.list, null);
TextView tv_id = stuView.findViewById(R.id.li_id);
TextView tv_name = stuView.findViewById(R.id.li_name);
TextView tv_academy = stuView.findViewById(R.id.li_academy);
ImageView iv_edit = stuView.findViewById(R.id.li_edit);
ImageView iv_delete = stuView.findViewById(R.id.li_delete);
final StuInfo student = (StuInfo) stuDates.get(position);
tv_id.setText(String.valueOf(student.getId()));
tv_name.setText(student.getName());
tv_academy.setText(student.getAcademy());
iv_edit.setImageResource(R.drawable.edit);
iv_delete.setImageResource(R.drawable.delete);
//增加监听
iv_edit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Context context = v.getContext();
Intent intent = new Intent();
intent.setClass(context, Edit.class);
intent.putExtra("altStu", student);
((Activity) context).startActivity(intent);
}
});
iv_delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dbOperate.delete(student.getId());
stuDates.remove(student);
StudentAdapter.this.notifyDataSetChanged();
}
});
//给删除和编辑设置标志
iv_edit.setTag(position);
iv_delete.setTag(position);
return stuView;
}
······
5.创建 searchActivity
public class searchActivity extends AppCompatActivity implements View.OnClickListener {
private Button btn_return;
private ListView lv_searchlist;
DBop dbOperate = new DBop();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
lv_searchlist = (ListView) findViewById(R.id.lv_searchlist);
btn_return = (Button) findViewById(R.id.btn_return);
btn_return.setOnClickListener(this);
dbOperate.test(this);
Bundle bundle = getIntent().getExtras();
String searchKey = bundle.getString("searchKey");
List<StuInfo> searchList = dbOperate.searchByAll(searchKey);
if (!searchList.isEmpty()) {
StudentAdapter myAdapter = new StudentAdapter(this, searchList);
lv_searchlist.setAdapter(myAdapter);
} else {
Toast.makeText(this, "无结果", Toast.LENGTH_SHORT);
}
}
······