码迷,mamicode.com
首页 > 移动开发 > 详细

Android : SQLite 版学生系统

时间:2020-01-21 18:19:41      阅读:121      评论:0      收藏:0      [点我收藏+]

标签:https   public   exe   oncreate   tac   extra   html   getname   position   

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 text\n," +
                "date text\n" +
                ");");
        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);
        }
    }
        ······

演示

技术图片技术图片

技术图片技术图片

代码下载地址

StudentDemo

Android : SQLite 版学生系统

标签:https   public   exe   oncreate   tac   extra   html   getname   position   

原文地址:https://www.cnblogs.com/esllovesn/p/12222488.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!