我是Android开发的初学者,我正在尝试将数据从游标加载到列表视图。但是,我得到了这个错误
12-23 22:51:12.711 16696-16696/com.chaos.todolist E/CursorWindow:
无法从包含两行的游标窗口中读取第1行第1列,
2列。
这是我的表演课

package com.chaos.todolist;

import android.database.Cursor;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class show extends AppCompatActivity {
    private  ListView list;
    private  DataBase db;
    private ArrayAdapter<String> adapter;
    private  Cursor result;
    private  String[] names;
    int[] ids;
    taskbody t;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.show);
        list=(ListView) findViewById(R.id.list);
        db=new DataBase(this);
         Log.d("show", "is going to load database");
     //   list.setOnItemClickListener((AdapterView.OnItemClickListener) this);
    }

    protected void onResume() {
        super.onResume();
        db.open();
        LoadDataFromDataBaseToListView();
    }
    protected  void onPause()
    {
        super.onPause();
    }

    private void LoadDataFromDataBaseToListView() {
        db.open();
        result=db.getAllTasks();
        if(result!=null) {
            int i = 0;
            names = new String[result.getCount()];
            ids = new int[result.getCount()];
            try {

                if (result.moveToFirst()) {
                    int index_id = result.getColumnIndex(t.COLUMN_ID);
                    int index_name = result.getColumnIndex(t.COLUMN_NAME);
                    while (result.moveToNext()) {
                        ids[i] = result.getInt(index_id);
                        names[i] = result.getString(index_name);
                        i++;
                    }
                    db.close();
                    adapter = new ArrayAdapter<String>(
                            getApplicationContext(),
                            R.layout.taskitem, R.id.name,
                            names);
                    list.setAdapter(adapter);
                }
            }catch (Exception e) {
                Toast.makeText(getApplicationContext(), e.getMessage(),
                        Toast.LENGTH_LONG).show();
            }
        }
        else {
            Toast.makeText(this, "is null", Toast.LENGTH_LONG).show();
            adapter = new ArrayAdapter<String>(
                    getApplicationContext(),
                    android.R.layout.simple_list_item_1);

            list.setAdapter(adapter);
        }
    }
 //  public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

   //   ViewTask(position);

    //}
    /*private void ViewTask(int position) {
        try {
            Intent intent = new Intent(show.this, ViewTask.class);
            intent.putExtra("_ID", ids[position]);
            Toast.makeText(getApplicationContext(), ids[position] + "",
                    Toast.LENGTH_LONG).show();
            startActivity(intent);
        } catch (Exception e) {
            Toast.makeText(getApplicationContext(), e.getMessage(),
                    Toast.LENGTH_LONG).show();
        }
    }*/

}

这个类包含get all tasks方法
package com.chaos.todolist;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class DataBase {

    private SQLiteDatabase mDatabase;
    private DB_Helper TODOHelper;

    public DataBase(Context context) {
        TODOHelper = new DB_Helper(context);
        open();
    }

    protected void open()
    {
        mDatabase = TODOHelper.getWritableDatabase();
    }

    public void close() {
        if (mDatabase != null) {
            mDatabase.close();
        }
    }

    public Cursor getAllTasks()
    {
        open();
        String[] col = { taskbody.COLUMN_ID, taskbody.COLUMN_NAME };
        Cursor cursor = mDatabase.query(
                taskbody.TABLE_TASKS, // table name
                col , // column names
                null, // where clause
                null, // where params
                null, // groupby
                null, // having
                taskbody.COLUMN_NAME // orderby
        );

        return cursor;
    }

    public Cursor getTaskById(int id) {
        String[] columns = { taskbody.COLUMN_NAME, taskbody.COLUMN_DETAILS,
                taskbody.COLUMN_ADDRESS};

        return mDatabase.query(
                taskbody.TABLE_TASKS, // table name
                columns, // column names
                taskbody.COLUMN_ID + " = " + id, // where clause // id param. could be here or appended as it is ^
                null, // where params
                null, // groupby
                null, // having
                null // orderby
        );
    }

    public void deleteTaskById(int id) {
        open();
        mDatabase.delete(
                taskbody.TABLE_TASKS, // table name
                taskbody.COLUMN_ID +"="+ id, // where clause
                null // where params
        );
        close();
    }

    public void insertTask(String name,String details,String address) {
        open();
        mDatabase.beginTransaction();
         try {
             ContentValues newTask = new ContentValues();
             newTask.put(taskbody.COLUMN_NAME, name);
             newTask.put(taskbody.COLUMN_DETAILS, details);
             newTask.put(taskbody.COLUMN_ADDRESS, address);
             mDatabase.insert(taskbody.TABLE_TASKS, null, newTask);
         } finally {
             mDatabase.endTransaction();
         }
        mDatabase.close();

        }


    public void updateTask(int id,String name,String details,String address) {
        open();
        ContentValues editTask = new ContentValues();
        editTask.put(taskbody.COLUMN_NAME, name);
        editTask.put(taskbody.COLUMN_DETAILS, details);
        editTask.put(taskbody.COLUMN_ADDRESS, address);
        mDatabase.update(
                taskbody.TABLE_TASKS, // table name
                editTask, // values
                taskbody.COLUMN_ID + " = " + id, // where clause
                null // where params
        );
        mDatabase.close();
    }
}

最佳答案

这个错误意味着两件事:
列不存在。
列的名称不正确。

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

10-10 09:12