我正在观看领域教程,并尝试在OnEditorActionListener
中实现代码。这就是代码。我使用的是Java 7,因此没有lambdas。 IDE说错误发生在新Realm
之前。 Transaction.Callback
所在的***
,以及预期的标识符。
myEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if(actionId == EditorInfo.IME_ACTION_DONE){
if (myEditText.getText()!=null){
Realm.Transaction myTr = new Realm.Transaction() {
@Override
public void execute(Realm realm) {
Task newTask = realm.createObject(Task.class);
newTask.setText(myEditText.getText().toString());
}
}, *** new Realm.Transaction.Callback(){
@Override
public void onSuccess() {
Log.d("REALM TASK", "saved successfully");
}
@Override
public void onError(Exception ignore) {
Log.d("REALM TASK", "not saved");
}
};
}
finish();
return true;
}
else { return false; }
}
});
我试过了:
Realm.Transaction.Callback myCallback = (Realm.Transaction.Callback) myTr.Callback;
答案 0 :(得分:1)
而不是
Realm.Transaction myTr = new Realm.Transaction() {
@Override
public void execute(Realm realm) {
Task newTask = realm.createObject(Task.class);
newTask.setText(myEditText.getText().toString());
}
}, *** new Realm.Transaction.Callback(){
@Override
public void onSuccess() {
Log.d("REALM TASK", "saved successfully");
}
@Override
public void onError(Exception ignore) {
Log.d("REALM TASK", "not saved");
}
};
应该是
final String text = myEditText.getText().toString();
realm.executeTransactionAsync(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
Task newTask = realm.createObject(Task.class);
newTask.setText(text);
}
}, new Realm.Transaction.Callback() {
@Override
public void onSuccess() {
Log.d("REALM TASK", "saved successfully");
}
@Override
public void onError(Exception e) {
Log.d("REALM TASK", "not saved", e);
}
});