『牛角书』基于鸿蒙的备忘录

ds
发布于 2022-12-13 00:13
浏览
1收藏

@toc

Guide

本项目为学校鸿蒙课程的课程实践设计,利用鸿蒙做一个简易的备忘录系统。

1.功能

1.1 创建新笔记

『牛角书』基于鸿蒙的备忘录-鸿蒙开发者社区

1.2 查看

『牛角书』基于鸿蒙的备忘录-鸿蒙开发者社区

1.3 选择排序

『牛角书』基于鸿蒙的备忘录-鸿蒙开发者社区

1.4 模糊搜索

『牛角书』基于鸿蒙的备忘录-鸿蒙开发者社区

2.准备工作

2.1 对象关系映射数据库

基本概念:HarmonyOS 对象关系映射(Object Relational Mapping,ORM)数据库是一款基于 SQLite 的数据库框架,屏蔽了底层 SQLite 数据库的 SQL 操作,针对实体和关系提供了增删改查等一系列的面向对象接口。应用开发者不必再去编写复杂的 SQL 语句, 以操作对象的形式来操作数据库,提升效率的同时也能聚焦于业务开发。

2.2 配置“build.gradle”文件

如果使用注解处理器的模块为“com.huawei.ohos.hap”模块,则需要在模块的“build.gradle”文件的“ohos”节点中添加以下配置:

    compileOptions {
        annotationEnabled true
    }

2.3 构造数据库,即创建数据库类并配置对应的属性

定义一个数据库类NoteStore.java,数据库包含了“Note"一个表,版本号为“1”。数据库类的 getVersion 方法和 getHelper 方法不需要实现,直接将数据库类设为虚类即可:

@Database(entities = {NoteBean.class}, version = 1)
public abstract class NoteStore extends OrmDatabase {
}

2.4 构造数据表

public class NoteBean extends OrmObject {
// 此处将id设为了自增的主键。注意只有在数据类型为包装类型时,自增主键才能生效。
    @PrimaryKey(autoGenerate = true)
    private Integer id;
    @Column
    private String title;
    @Column
    private String content;
    @Column
    private String addTime;
    @Column
    private String updateTime;
    // 开发者自行添加字段的getter和setter方法。
}

3.功能实现

3.1 跳转页面

ivWriteNote.setClickedListener(component -> {
            //Intent 对象之间传递信息的载体
            Intent i = new Intent();
            //不需要传参的时候使用OperationBuilder,自定义传参使用Parameters
            Operation operationBuilder = new Intent.OperationBuilder()
                    .withDeviceId("")//跳转哪个设备,空代表当前设备
                    .withBundleName("com.example.studydemo.hmosnote")//跳转哪个应用
                    .withAbilityName("com.example.studydemo.hmosnote.WriteNoteAbility")//跳转哪个页面
                    .build();
            // 把operation设置到intent中
            i.setOperation(operationBuilder);
            //
            startAbility(i);
        });

3.2 代码实现

WriteNoteAbility

public class WriteNoteAbility extends Ability {
    private NoteBean noteBean;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_ability_write_note);
        String note = intent.getStringParam("note");
        if (!TextTool.isNullOrEmpty(note)) {
            noteBean = new Gson().fromJson(note, NoteBean.class);
        }
        Text tvTiltle = (Text) findComponentById(ResourceTable.Id_tv_title);
        tvTiltle.setText(noteBean == null ? "创建" : "编辑");
        Image ivBack = (Image) findComponentById(ResourceTable.Id_iv_back);
        Image ivRight = (Image) findComponentById(ResourceTable.Id_iv_right);
        TextField tfTitle = (TextField) findComponentById(ResourceTable.Id_tf_title);
        TextField tfContent = (TextField) findComponentById(ResourceTable.Id_tf_content);
        ivBack.setVisibility(Component.VISIBLE);
        ivRight.setVisibility(Component.VISIBLE);
        ivBack.setClickedListener(component -> {
            terminateAbility();
        });
        ivRight.setClickedListener(component -> {
            if (noteBean == null) {
                noteBean = new NoteBean();
                noteBean.setTitle(tfTitle.getText());
                noteBean.setContent(tfContent.getText());
                String time = new SimpleDateFormat("yyyy-MM-dd HH:mm").format(new Date());
                noteBean.setAddTime(time);
                noteBean.setUpdateTime(time);
                DBUtils.getInstance(this).insert(noteBean);
                terminateAbility();
            } else {
                noteBean.setTitle(tfTitle.getText());
                noteBean.setContent(tfContent.getText());
                String time = new SimpleDateFormat("yyyy-MM-dd HH:mm").format(new Date());
                noteBean.setUpdateTime(time);
                DBUtils.getInstance(this).update(noteBean);
            }
        });
        if (note != null) {
            tfTitle.setText(noteBean.getTitle());
            tfContent.setText(noteBean.getContent());
        }
    }
}

insert操作

public void insert(NoteBean noteBean) {
        OrmContext ormContext = helper.getOrmContext("note_deom", "note_demo.db", NoteStore.class);
        if (ormContext.insert(noteBean)) {
            showToast("操作成功");
        } else {
            showToast("操作失败");
        }
        ormContext.registerContextObserver(ormContext, contextOrmObjectObserver);
        ormContext.flush();
        ormContext.close();
    }

query操作

public List<NoteBean> query(String keyWord, int orderType) {
        OrmContext ormContext = helper.getOrmContext("note_demo", "note_demo.db", NoteStore.class);
        OrmPredicates ormPredicates = ormContext.where(NoteBean.class);
        if (keyWord != null && keyWord.length() > 0) {
            ormPredicates = ormPredicates.like("title", String.format("%%%s%%",keyWord)).or().like("content", String.format("%%%s%%",keyWord));
        }
        if (orderType == 1) {
            ormPredicates.orderByDesc("addTime");
        }
        if (orderType == 2) {
            ormPredicates.orderByDesc("updateTime");
        }
        if (orderType == 3) {
            ormPredicates.orderByAsc("title");
        }
        List<NoteBean> query = ormContext.query(ormPredicates);
        HiLog.info(LABEL_LOG, query.size() + "");
        return query;
    }

分类
标签
3
收藏 1
回复
举报
1条回复
按时间正序
/
按时间倒序
红叶亦知秋
红叶亦知秋

不错,便签还是经常会用到的

回复
2022-12-13 10:45:41
回复
    相关推荐