我正試圖在我的應用程序中實現收藏夾列表。爲此,我使用領域作爲我的數據庫來存儲內容。我能夠成功添加內容,但試圖刪除項目時會使應用程序崩潰。我試圖刪除使用主鍵是movieId變量的對象。但它會使應用程序崩潰。 任何幫助被讚賞。android-如何添加和刪除對象到realm數據庫?
代碼中添加和刪除的內容:
mFavoriteButton.setOnFavoriteChangeListener(new MaterialFavoriteButton.OnFavoriteChangeListener() {
@Override
public void onFavoriteChanged(MaterialFavoriteButton buttonView, boolean favorite) {
if(!favorite) {
SharedPreferences.Editor editor = getSharedPreferences("com.thejoker.yts", MODE_PRIVATE).edit();
editor.putBoolean("Fav Checked", false);
editor.commit();
RealmResults<FavoriteListRealm> results = mRealm.where(FavoriteListRealm.class).equalTo("realmMovieId", movieId).findAll();
mRealm.beginTransaction();
mResults.remove(results);
mRealm.commitTransaction();
Toast.makeText(getContext(), "Not a favorite", Toast.LENGTH_SHORT).show();
}
if(favorite) {
SharedPreferences.Editor editor = getSharedPreferences("com.thejoker.yts", MODE_PRIVATE).edit();
editor.putBoolean("Fav Checked", true);
editor.commit();
mRealm.beginTransaction();
FavoriteListRealm favorites = mRealm.createObject(FavoriteListRealm.class);
favorites.setRealmMovieId(movieId);
favorites.setRealmMovieTitle(movieTitle);
favorites.setRealmThumbnailUrl(movieUrlThumbnail);
favorites.setRealmMovieYear(movieYear);
mRealm.copyToRealmOrUpdate(favorites);
mRealm.commitTransaction();
mResults = mRealm.where(FavoriteListRealm.class).findAllAsync();
Toast.makeText(MovieDetailsActivity.this, mResults.toString(), Toast.LENGTH_LONG).show();
}
}
});
問題是什麼?請清楚! – cramopy
如何從Realm中刪除對象。那是我的問題 –