2015-12-23 152 views
1

我是Android開發人員的初學者,我試圖從光標加載數據到列表視圖。然而,我得到這個錯誤無法從具有2行,2列的CursorWindow中讀取第1行第-1列

12-23 22:51:12.711 16696-16696/com.chaos.todolist E/CursorWindow: 無法讀取行1,列-1從具有2行的CursorWindow , 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(); 
     } 
    }*/ 

} 

這是一個包含讓所有任務的方法

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(); 
    } 
} 
+0

哪條線給你錯誤? – Abdelhak

+0

你的結果似乎有些問題。 –

回答

1

此錯誤Failed to read row 1, column -1 from a CursorWindow which has 2 rows, 2 columns意味着拖東西:

  1. 的列不存在。
  2. 該列的名稱不正確。
+0

我檢查數據庫,然後創建問題解決 – zod101

相關問題