我在recyclerview中删除对象时遇到问题。当我单击RV项目中的按钮时,出现错误:java.lang.IllegalArgumentException:非法参数:该字段(id)不可为空。 RealmHelper的这一行有错误:RealmResults results = realm.where(Task.class).equalTo(“ id”,taskID).findAll();
MainActivity:
公共类MainActivity扩展了AppCompatActivity,实现了IRecyclerItemClickListener {
@Nullable
@BindView(R.id.floating_button) FloatingActionButton floatingButton;
RecyclerView recyclerView;
MainAdapter mainAdapter;
Realm realm;
RealmHelper realmHelper;
RealmResults<Task> results;
List<Task> tasks;
String taskID;
int position;
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
recyclerView = findViewById(R.id.recycler_view);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
Realm.init(this);
RealmConfiguration configuration = new RealmConfiguration
.Builder()
.deleteRealmIfMigrationNeeded()
.build();
realm = Realm.getInstance(configuration);
realmHelper = new RealmHelper(realm);
tasks = new ArrayList<>();
tasks = realmHelper.getAllTasks();
mainAdapter = new MainAdapter(tasks,this);
recyclerView.setAdapter(mainAdapter);
onDeleteClick(position);
}
@Optional
@OnClick(R.id.floating_button)
public void onClick() {
android.app.Fragment fragment = new TaskFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.main_activity, fragment);
transaction.addToBackStack(null);
transaction.commit();
}
public void showFloatingActionButton() {
floatingButton.show();
}
public void hideFloatingActionButton() {
floatingButton.hide();
}
@Override
public void onBackPressed() {
super.onBackPressed();
showFloatingActionButton();
recyclerView.removeAllViews();
}
@Override
protected void onDestroy() {
realm.close();
super.onDestroy();
}
@Override
public void onDeleteClick(int position) {
realmHelper.deleteTask(taskID, position);
Log.d(TAG, "ON DELETE CLICK " + taskID);
}
}
RealmHelper:
public class RealmHelper {
Realm realm;
private static ArrayList<Task> taskArrayList = new ArrayList<>();
private static final String TAG = "RealmHelper";
MainAdapter mainAdapter;
public RealmHelper(Realm realm) {
this.realm = realm;
}
public void saveTask(final Task task) {
realm.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
Task t = realm.copyToRealm(task);
}
});
}
public List<Task> getAllTasks() {
RealmResults<Task> taskResult = realm.where(Task.class).findAll();
return taskResult;
}
public void deleteTask( String taskID, int position) {
RealmResults<Task> results = realm.where(Task.class).equalTo("id", taskID).findAll();
if (results.size() != 0) {
realm.beginTransaction();
results.deleteAllFromRealm();
realm.commitTransaction();
taskArrayList.remove(position);
mainAdapter.notifyDataSetChanged();
}
}
}
答案 0 :(得分:1)
在MainActivity
中,您定义了taskID
字段,但从未对其进行初始化。这意味着它具有字符串的默认值null
。
我猜您的Task
列表中的每个tasks
对象都具有某种ID。由于您已经有了位置参数,因此可以使用它即时获取ID。
@Override
public void onDeleteClick(int position) {
Task task = tasks.get(position);
String id = task.getTaskID(); // or whatever the correct call is
realmHelper.deleteTask(id, position);
Log.d(TAG, "ON DELETE CLICK " + taskID);
}