简体   繁体   English

无法从具有3行,3列的CursorWindow中读取第0行,第3列

[英]Failed to read row 0, column 3 from a CursorWindow which has 3 rows, 3 columns

I know this question has been asked before, but I can't seem to find the answer in the other posts about this. 我知道之前曾有人问过这个问题,但我似乎在其他文章中找不到答案。 I have the error "Failed to read row 0, column 3 from a CursorWindow which has 3 rows, 3 columns" and with that "Couldn't read row 0, col 3 from CursorWindow.". 我有错误“无法从具有3行,3列的CursorWindow读取第0行,第3列”,并且出现错误“无法从CursorWindow读取第0行,第3列”。 I'm trying to make a to-do list for Android. 我正在尝试为Android做一个待办事项清单。 I'm not sure why this error is coming, cause I know that my DB has 4 columns (0 to 3) like the error says I'm trying to read row 0 column 3 (which should be possible, since there are 3 columns) and still it gives me the error. 我不确定为什么会出现此错误,因为我知道我的数据库有4列(0到3),就像该错误说我正在尝试读取第0行第3列(应该可以,因为有3列) ),但仍然给我错误。 Below is the code (it's separated in files, I'll put the file where the error occurs first (btw, it on: String taskReminder = cursor.getString(reminderColumnIndex); 下面是代码(将其分隔在文件中,我将首先在该错误发生的位置放置该文件(顺便说一句,它位于: String taskReminder = cursor.getString(reminderColumnIndex);

Hope you guys can help me out, and sorry for asking a question which was asked before, but I really couldn't find the answer in the other posts. 希望你们能帮助我,并很抱歉提出以前提出的问题,但我真的无法在其他帖子中找到答案。

Edited: I think this is the code you need: 编辑:我认为这是您需要的代码:

CatalogActivity.java 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);
    }
}

Also, I think, I found the solution. 另外,我认为,我找到了解决方案。 Correct me if I'm wrong: in the String[] projection, I only have, apart from the ID, the name and if it's completed. 如果我错了,请纠正我:在String []投影中,除了ID之外,我仅拥有名称以及名称是否完整。 I'm guessing here needs to be the reminderdate as well? 我猜这里也需要提醒一下吗?

Edited: Yes, solved my own problem. 编辑:是的,解决了我自己的问题。 I put the reminderdata in the String[] projection and now it works. 我将提醒数据放在String []投影中,现在可以使用了。 I couldn't have done it without you guys (you pointed me in the right direction), so thank you very mutch. 没有你们,我做不到(您向正确的方向指出了我),所以非常感谢您。 You are the best! 你是最棒的!

Column indexing is zero-based. 列索引从零开始。 A Cursor with 3 columns has values at indexes 0, 1 and 2. That would explain the exception as listed in your question title. 具有3列的游标的索引值为0、1和2。这将解释问题标题中列出的异常。

The code you posted does not seem to be the code that produces that exception. 您发布的代码似乎不是产生该异常的代码。 You obtain column index with getColumnIndex() which returns -1 for columns that are not in the cursor. 您可以使用getColumnIndex()获得列索引,该列索引为不在光标中的列返回-1。 It never returns an index that is not in the cursor. 它从不返回不在游标中的索引。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM