我有一个代码可以一次删除所有离线保存的帖子,我想知道是否有人可以帮助我如何一次只删除所选帖子。
以下是一键删除所有帖子的代码。
private void deleteAllPosts() {
new AlertDialog.Builder(getActivity())
.setMessage("Are you sure you want to delete all?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
final RealmResults<RealmModel> results = realm.where(RealmModel.class).findAll();
realm.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
results.deleteAllFromRealm();
posts.clear();
postAdapter.notifyItemRangeChanged(0, posts.size() - 1);
rvPostRecyclerView.setAdapter(postAdapter);
rootView.findViewById(R.id.showLoading).setVisibility(View.VISIBLE);
}
});
Snackbar.make(rvPostRecyclerView, "Successfully deleted all posts ", Snackbar.LENGTH_LONG).show();
}
})
.setNegativeButton("No", null)
.show();
}
}
答案 0 :(得分:0)
例如,如果要删除帖子1和2,
“post”是字段名称,“1”&amp; “2”是值。你可以在数组中包含任意数量的值。
final RealmResults<RealmModel> results = realm.where(RealmModel.class).in("post", ["1","2"]).findAll();
有关更多相关问题,请查看here