java - 无法从具有 3 行、3 列的 CursorWindow 读取第 0 行、第 3 列

标签 java android database

我知道这个问题之前已经被问过,但我似乎无法在其他帖子中找到答案。我收到错误“无法从具有 3 行、3 列的 CursorWindow 读取第 0 行、第 3 列”,并且出现“无法从 CursorWindow 读取第 0 行、第 3 列。”。我正在尝试为 Android 制作一个待办事项列表。我不确定为什么会出现这个错误,因为我知道我的数据库有 4 列(0 到 3),就像错误说我正在尝试读取第 0 行第 3 列(这应该是可能的,因为有 3 列)但它仍然给我错误。下面是代码(它被分成文件,我将把文件放在最先发生错误的位置(顺便说一句,它放在:String taskReminder = curve.getString(reminderColumnIndex);

希望你们能帮助我,很抱歉问了一个以前被问过的问题,但我真的在其他帖子中找不到答案。

编辑:我认为这是您需要的代码:

CatalogActivity.java

some imports...
public class CatalogActivity extends AppCompatActivity implements
        LoaderManager.LoaderCallbacks<Cursor> {

    private static final int TASK_LOADER = 0;

    CursorAdapter mCursorAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_catalog);

        FloatingActionButton fab = findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(CatalogActivity.this, EditorActivity.class);
                startActivity(intent);
            }
        });

        ListView taskListView = findViewById(R.id.list);

        View emptyView = findViewById(R.id.empty_view);
        taskListView.setEmptyView(emptyView);

        mCursorAdapter = new CursorAdapter(this, null);
        taskListView.setAdapter(mCursorAdapter);

        taskListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
                Intent intent = new Intent(CatalogActivity.this, EditorActivity.class);

                Uri currentTaskUri = ContentUris.withAppendedId(Entry.CONTENT_URI, id);

                intent.setData(currentTaskUri);

                startActivity(intent);
            }
        });

        getLoaderManager().initLoader(TASK_LOADER, null, this);
    }

    private void deleteAllTasks() {
        int rowsDeleted = getContentResolver().delete(Entry.CONTENT_URI, null, null);
        Log.v("CatalogActivity", rowsDeleted + " rows deleted from task database");
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_catalog, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_insert_dummy_data:
                insertTask();
                return true;
            case R.id.action_delete_all_entries:
                deleteAllTasks();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public Loader<Cursor> onCreateLoader(int i, Bundle bundle) {
        String[] projection = {
                Entry._ID,
                Entry.COLUMN_TASK_NAME,
                String.valueOf(Entry.COLUMN_TASK_COMPLETED)};

        return new CursorLoader(this,   // Parent activity context
                Entry.CONTENT_URI,      // Provider content URI to query
                projection,             // Columns to include in the resulting Cursor
                null,                   // No selection clause
                null,                   // No selection arguments
                null);                  // Default sort order
    }

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
        mCursorAdapter.swapCursor(data);
    }

    @Override
    public void onLoaderReset(Loader<Cursor> loader) {
        mCursorAdapter.swapCursor(null);
    }
}

而且,我想,我找到了解决方案。如果我错了,请纠正我:在 String[] 投影中,除了 ID 之外,我只有名称以及它是否已完成。我猜这里也需要是提醒日期?

编辑:是的,解决了我自己的问题。我将提醒数据放入 String[] 投影中,现在它可以工作了。没有你们,我不可能做到这一点(你们为我指明了正确的方向),所以非常感谢你们。你是最棒的!

最佳答案

列索引是从零开始的。具有 3 列的游标在索引 0、1 和 2 处具有值。这可以解释问题标题中列出的异常。

您发布的代码似乎不是产生该异常的代码。您可以使用 getColumnIndex() 获取列索引,对于不在游标中的列返回 -1。它永远不会返回不在游标中的索引。

关于java - 无法从具有 3 行、3 列的 CursorWindow 读取第 0 行、第 3 列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48963152/

相关文章:

Android 媒体播放器服务最佳实践

Java 类加载器 : why search the parent classloader first?

java - JSR 303 自定义约束覆盖

从另一个线程调用的Java方法,数据更新不是即时的

android - 如何使用 invokeMethod 从 Native 传递字符串数组到 flutter

android - 如何从 ESHA Research API 调用多个营养信息? (apid.esha.com)

database - PostgreSQL - 检查两个属性中的值是否相等

sql - 导出数据库的 CREATE 脚本

javascript - 通过更好的排序加速 MongoDB find()? (使用 Mongoose orm)

java - 没有缓存版本的 com.android.tools.build :aapt2:3. 2.0-alpha18-4804415 可用于离线模式