有没有办法从setViewValue
代码手动更新游标的项目值?目前我只是再次获取所有记录,然后changeCursor
和notifyDataSetChanged
,但速度非常慢:
dbHelper = new DbAdapter(this);
dbHelper.open();
recordsCursor = dbHelper.fetchAllRecords();
startManagingCursor(recordsCursor);
String[] from = new String[]{DbAdapter.KEY_1, DbAdapter.KEY_2};
int[] to = new int[]{R.id.text1, R.id.background};
adapter = new SimpleCursorAdapter(this, R.layout.row, recordsCursor, from, to);
if (adapter != null) {
adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
@Override
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
int viewId = view.getId();
switch(viewId) {
case R.id.text1:
final Object item = adapter.getItem(cursor.getPosition());
...
// some logic is here, so the code below is run only when item is displayed for 5 seconds
dbHelper.open();
Boolean result = dbHelper.markRecordAsRead(text); // update the database
...
// the code below works, but since I read the whole database, it is SLOW
dbHelper.open();
recordsCursor = dbHelper.fetchAllRecords();
adapter.changeCursor(recordsCursor);
adapter.notifyDataSetChanged();
// is there any way just to update the cursor's item value?