CursorIndexOutOfBoundsException:请求的索引0,在android Sqlite数据库中的大小为0

时间:2018-06-26 05:11:36

标签: android sqlite listview

我试图在 sqlite数据库中的列表视图 当我在数据库中添加值时 它给出了错误

android.database.CursorIndexOutOfBoundsException: 

请求索引0,大小为0 我也把条件检查相同的值 这是我的DatabaseHelper班。

如何解决此错误?

DatabaseHelper.java

public class DatabaseHelper extends SQLiteOpenHelper {
Context context;
private static final int DATABASE_VERSION = 7;
private static final String DATABASE_NAME = "db_notes";
public DatabaseHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL(Note.CREATE_TABLE);
    this.context = context;
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + Note.TABLE_NAME);
    onCreate(db);
 }
 public String insertNote(String note, String address, Context context) {
    SQLiteDatabase db  = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    Cursor resultSet = db.rawQuery(" SELECT " + Note.COLUMN_NOTE + " FROM " +
            Note.TABLE_NAME + " WHERE " +
    Note.COLUMN_NOTE + " = \"" + note + 
    "\"" ,null,null);
    if (resultSet.moveToFirst())
    {
        values.put(Note.COLUMN_NOTE, note);
        values.put(Note.COLUMN_ADDRESS, address);
        db.insert(Note.TABLE_NAME, null, values);
    }
    else Toast.makeText(context,note+" already 
    exists",Toast.LENGTH_SHORT).show();
    return note;
 }
  public Note getNote(String id) {
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.query(Note.TABLE_NAME,
            new String[]{Note.COLUMN_ID, 
    Note.COLUMN_NOTE,Note.COLUMN_ADDRESS, Note.COLUMN_TIMESTAMP},
            Note.COLUMN_ID + "=?",
            new String[]{String.valueOf(id)}, null, null, null, null);
    if (cursor.moveToFirst())
        cursor.moveToFirst();
        Note note = new Note(
                cursor.getInt
       (cursor.getColumnIndex(Note.COLUMN_ID))cursor.getString(cursor
       .getColumnIndex(Note.COLUMN_NOTE)),
                cursor.getString(cursor.getColumnIndex(Note.COLUMN_ADDRESS)),
                cursor.getString
           (cursor.getColumnIndex(Note.COLUMN_TIMESTAMP)));
        cursor.close();
        return note;
}
public List<Note> getAllNotes() {
    List<Note> notes = new ArrayList<>();
    String selectQuery = "SELECT  * FROM " + Note.TABLE_NAME + " ORDER BY " +
            Note.COLUMN_TIMESTAMP + " DESC";
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);
    if (cursor.moveToFirst()) {
        do {
            Note note = new Note();
            note.setId(cursor.getInt(cursor.getColumnIndex(Note.COLUMN_ID)));

note.setNote(cursor.getString(cursor.getColumnIndex(Note.COLUMN_NOTE)));
note.setAddress(cursor.getString
(cursor.getColumnIndex(Note.COLUMN_ADDRESS)));
note.setTimestamp(cursor.getString
 (cursor.getColumnIndex(Note.COLUMN_TIMESTAMP)));
            notes.add(note);
        } while (cursor.moveToNext());
    }
    cursor.close();
    db.close();
    return notes;
}
}

logcat错误:

 android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
        at android.database.AbstractCursor.checkPosition(AbstractCursor.java:460)
        at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
        at android.database.AbstractWindowedCursor.getInt(AbstractWindowedCursor.java:68)
        at info.androidhive.sqlite.database.DatabaseHelper.getNote(DatabaseHelper.java:63)

2 个答案:

答案 0 :(得分:0)

您的检查不起作用:

if (cursor.moveToFirst())
    cursor.moveToFirst();
...

如果不满足条件,则代码将在您无条件读取游标的第二个(冗余)moveToFirst()之后继续执行。改用类似这样的东西:

if (cursor.moveToFirst()) {
    ...
}

答案 1 :(得分:0)

***// database class***

package com.example.dbsample;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import java.util.ArrayList;
import java.util.List;

public class DatabaseHandler extends SQLiteOpenHelper {

    // All Static variables
    // Database Version
    private static final int DATABASE_VERSION = 1;

    // Database Name
    private static final String DATABASE_NAME = "noteManager";

    // ContactModel Table Columns names
    private static final String TABLE_NOTES = "table_send";
    private static final String NOTES_ID = "note_id";
    private static final String NOTES_NAME= "note_name";
    private static final String NOTES_ADDRESS = "address";

    public DatabaseHandler(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }
   //
    @Override
    public void onCreate(SQLiteDatabase db) {

        String CREATE_NOTES_TABLE = "CREATE TABLE " + TABLE_NOTES + "("
                + NOTES_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
                + NOTES_NAME + " TEXT," + NOTES_ADDRESS + " TEXT" +
                ")";

        db.execSQL(CREATE_NOTES_TABLE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }

    public void addNOTES(Notes notes) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();

        values.put(NOTES_NAME, notes.getName());
        values.put(NOTES_ADDRESS, notes.getAddress());

        db.insertWithOnConflict(TABLE_NOTES, null, values,SQLiteDatabase.CONFLICT_REPLACE);
        db.close(); // Closing database connection
    }

    public List<Notes> getNotes() {
        List<Notes> notesList = new ArrayList<>();
        // Select All Query
        String selectQuery = "SELECT  * FROM " + TABLE_NOTES;

        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                Notes notes = new Notes();
                notes.setNote_id(cursor.getInt(0));
                notes.setName(cursor.getString(1));
                notes.setAddress(cursor.getString(2));

                notesList.add(notes);
            } while (cursor.moveToNext());
        }
        db.close();

        // return contact list
        if(notesList.size()>0) {

            return notesList;
        }
        else
        {
            return null;
        }
    }

    public void deleteNotes()
    {
        SQLiteDatabase db = this.getWritableDatabase();
        db.execSQL("DELETE FROM " + TABLE_NOTES);
        db.close();
    }

}

***// MainActivity***



  package com.example.dbsample;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

import java.util.List;

public class MainActivity extends AppCompatActivity {

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

        DatabaseHandler db= new DatabaseHandler(MainActivity.this);

        // programmatically change values

        db.addNOTES(new Notes("Your_note","Your_address"));

        List<Notes> notesList =  db.getNotes();

        for(int i=0;i<notesList.size();i++)
        {
            Log.d("DATABASE NOTE :", notesList.get(i).getName());
            Log.d("DATABASE ADDRESS :", notesList.get(i).getAddress());
        }

    }
}

***//Notes.java***



 package com.example.dbsample;

public class Notes {
    int note_id;
    String name;
    String address;

    public Notes() {
    }

    public Notes(String name, String address) {
        this.name = name;
        this.address = address;
    }

    public int getNote_id() {
        return note_id;
    }

    public void setNote_id(int note_id) {
        this.note_id = note_id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
}