gpt4 book ai didi

android - 在 Realm 的 recyclerview 中删除对象的问题

转载 作者:行者123 更新时间:2023-11-29 23:36:39 24 4
gpt4 key购买 nike

我在删除 recyclerview 中的对象时遇到问题。当我单击 RV 项目中的按钮时出现错误:java.lang.IllegalArgumentException:非法参数:此字段 (id) 不可为空。 RealmHelper 中的这一行有错误:RealmResults results = realm.where(Task.class).equalTo("id", taskID).findAll();

主要 Activity :

公共(public)类 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);

}

Realm 助手:

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();
}

}

最佳答案

MainActivity 中,您定义了一个 taskID 字段,但从未对其进行初始化。这意味着它具有字符串的默认值,即 null

我猜你的 tasks 列表中的每个 Task 对象都有一个某种类型的 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);
}

关于android - 在 Realm 的 recyclerview 中删除对象的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52173670/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com