java 适用于Android的领域数据库

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 适用于Android的领域数据库相关的知识,希望对你有一定的参考价值。

// Make the tasks class extend RealmObject

private Realm mRealm;
RealmResults<tasks> results;

Realm.init(MainActivity.this);
mRealm = Realm.getDefaultInstance();

results = mRealm.where(tasks.class).findAll();

//This listener will be called whenever there's a change in the realm database

RealmChangeListener realmChangeListener = new RealmChangeListener() {
    @Override
    public void onChange(Object element) {
        displayList();
    }
};

results.addChangeListener(realmChangeListener);

//Create a row in realm Database

mRealm.beginTransaction();
tasks task = mRealm.createObject(tasks.class);
task.setTaskTitle(et.getText().toString());
task.setTaskPriority(i);
task.setCompleted(false);
task.setTaskTime(Calendar.getInstance().getTime().toString());
task.setTaskID(UUID.randomUUID().toString());
mRealm.commitTransaction();

//Delete a particular row from realm

tasks removeTask = mRealm
                    .where(tasks.class)
                    .equalTo("taskID", tasksList.get(position).getTaskID())
                    .findFirst(); //Here taskID is the column name
                    
if (!mRealm.isInTransaction()) {
    try {
        mRealm.beginTransaction();
        removeTask.deleteFromRealm();
        mRealm.commitTransaction();
    } catch (Exception e) {
        mRealm.cancelTransaction();
    }
} else
    mRealm.cancelTransaction();
    

@Override
public void onDestroy() {
    super.onDestroy();
    mRealm.close();
}
    

以上是关于java 适用于Android的领域数据库的主要内容,如果未能解决你的问题,请参考以下文章

java 适用于Android的Thread,Hander实现

适用于 Android 的 SSH Java 库? [关闭]

哪些 Java SE 版本适用于 android studio 3.1?

java 适用于Android动画的更具配置性的反弹内插器

适用于 Android 的突变测试工具

java 适用于Android RecyclerView的简单基于RxJava的适配器