我想使用领域数据库开发todo应用程序。我已经成功创建了一个列表并显示在回收者视图中。但是当我尝试在回收者视图项目中添加任务时。我有点困惑。我不知道该怎么实现。
例如:
这是我的POJO课
import io.realm.RealmList;
import io.realm.RealmObject;
import io.realm.annotations.PrimaryKey;
public class ShoppingModel extends RealmObject {
@PrimaryKey
private int id;
private String title;
private String items;
private String color;
private String date;
private String time;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getItems() {
return items;
}
public void setItems(String items) {
this.items = items;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
}
这是我的领域帮助者课程
import android.util.Log;
import java.util.List;
import io.realm.Realm;
import io.realm.RealmList;
import io.realm.RealmResults;
public class RealmHelper {
Realm realm;
public RealmHelper(Realm realm){
this.realm = realm;
}
//save data to realm
public void save(final ShoppingModel shoppingModel){
realm.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
if (realm!=null){
Log.e("Log", "Database was created " );
Number currentID = realm.where(ShoppingModel.class).max("id");
int nextId;
if (currentID == null){
nextId=1;
} else {
nextId = currentID.intValue() + 1;
}
shoppingModel.setId(nextId);
ShoppingModel s = realm.copyToRealm(shoppingModel);
} else {
Log.e("Log", "Database not exits " );
}
}
});
}
public List<ShoppingModel> getAllShoppingList(){
RealmResults<ShoppingModel> shopResult = realm.where(ShoppingModel.class).findAll();
return shopResult;
}
//update data to realm
public void update(final int id, final String title, final String items, final String color, final String date, final String time){
realm.executeTransactionAsync(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
ShoppingModel shoppingModel = realm.where(ShoppingModel.class).equalTo("id",id)
.findFirst();
shoppingModel.setTitle(title);
shoppingModel.setItems(items);
shoppingModel.setTime(time);
shoppingModel.setDate(date);
shoppingModel.setColor(color);
}
}, new Realm.Transaction.OnSuccess(){
@Override
public void onSuccess() {
}
}, new Realm.Transaction.OnError(){
@Override
public void onError(Throwable error) {
error.printStackTrace();
}
});
}
// delete from realm
public void delete(final int id, final String title, final String items, final String color, final String date, final String time){
realm.executeTransactionAsync(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
ShoppingModel shoppingModel = realm.where(ShoppingModel.class).equalTo("id",id)
.findFirst();
shoppingModel.setTitle(title);
shoppingModel.setItems(items);
shoppingModel.setTime(time);
shoppingModel.setDate(date);
shoppingModel.setColor(color);
shoppingModel.deleteFromRealm();
realm.commitTransaction();
}
}, new Realm.Transaction.OnSuccess(){
@Override
public void onSuccess() {
}
}, new Realm.Transaction.OnError(){
@Override
public void onError(Throwable error) {
error.printStackTrace();
}
});
}
}
这是我的输出
现在,我想单击该项目中要添加复选框的项目
例如这样的
答案 0 :(得分:0)
List和ShoppingModel都不能直接存储在Realm中。您将需要为ShoppingModel创建一个模型对象,然后使用包含您的模型对象的RealmList对象。
public class ShoppingModel extends RealmObject {
public ShoppingModel() { }
@PrimaryKey
private int id;
private String title;
private String items;
private String color;
private String date;
private String time;
RealmList<ShoppingModelDetails> shoppingListDetails = new RealmList<ShoppingModelDetails>();
// getter setters are here.
}
ShoppingModelDetails类在这里-
public class ShoppingModelDetails extends RealmObject {
public ShoppingModelDetails() { }
@PrimaryKey
private int id;
private String title;
private String items;
private String color;
private String date;
private String time;
// getter setters are here.
}
///将购物模型详细信息对象添加到shoppingListDetails并将其存储在ShoppingList对象中 从数据库插入/检索数据库时,需要将ShoppingModelDetails类的对象手动转换为ShoppingModel类。