gpt4 book ai didi

android - 房间 : how to convert to SQLite method to Dao's @Query

转载 作者:太空狗 更新时间:2023-10-29 14:35:16 25 4
gpt4 key购买 nike

我有一个 CardView 的 RecyclerView 列表。下面的方法运行一个数据库方法,该方法使用“for”循环来更新索引值的排序顺序列。如果列表中有 9 个 CardView,则排序顺序列的值将从 9 开始并降到 1。

public void resetSortIndexes() {

int index = List.size();
for (ListItem s : List) {
s.setSortorder(index);
// update the SQLite database with the new,
// descending Sort numbers.
sqLiteDB.updateSortIndexes(s);
index--;
}
}

Database
...
public void updateSortIndexes (ListItem item) {

ContentValues values = new ContentValues();
values.put(ItemContract.ItemEntry.COLUMN_SORTORDER, item.getSortorder());

db.update(TABLE_NAME,values,ItemContract.ItemEntry.COLUMN_ID + " = ?",new String[]{String.valueOf(item.getId())});
}

我正在尝试使用 Dao 方法在 Room 中复制相同的方法和数据库更新,但没有成功。我在这里缺少什么?

public void resetSortIndexes() {

int sortId = -1;
int sortOrder = -1;
int index = List.size();
for (Card q : List) {
q.setSortorder(index);
sortOrder = q.getSortorder();
sortId = q.getId();
// update the Room database with the new, descending Sort index numbers.
mCardViewModel.updateSortOrder(sortOrder, sortId);
index--;
}
}

ViewModel
...
public void updateSortOrder(int sortOrder, int sortId) {
repository.updateSortOrder(sortOrder, sortId);
}

Repository
...
public void updateSortOrder(int sortOrder, int sortId) {
try {
new UpdateSortOrderColAsyncTask(quickcardDao).execute(sortOrder, sortId).get();
} catch (ExecutionException | InterruptedException e) {
e.printStackTrace();
}
}

private static class UpdateSortOrderColAsyncTask extends AsyncTask<Integer, Void, Void> {

private QuickcardDao asyncTaskDao;

UpdateSortOrderColAsyncTask(QuickcardDao dao) {
asyncTaskDao = dao;
}

@Override
protected Void doInBackground(final Integer... params) {

asyncTaskDao.updateSortorder(params[0],params[1]);
return null;
}
}

Dao
...
@Query("UPDATE quickcards SET quickcardSortorder = :sortOrder WHERE quickcardId = :sortId")
void updateSortorder(int sortOrder, int sortId);

最佳答案

原来需要更新 RecyclerView 的适配器。我将此方法添加到 removeItem() 中:

adapter.remove(quickcard);

Adapter
...
public void remove(Card item) {

int position = mCards.indexOf(item);

if (mCards.size()==0) {
mCards.clear();
}
else if (position > -1) {
mCards.remove(position);
}
}

The updated (and correct) RecyclerView list size
then allowed resetSortIndexes() to run properly.

关于android - 房间 : how to convert to SQLite method to Dao's @Query,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57351079/

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