Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/187.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
CursorIndexOutOfBoundsException:请求索引0,在android Sqlite数据库中大小为0_Android_Sqlite_Listview - Fatal编程技术网

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

CursorIndexOutOfBoundsException:请求索引0,在android Sqlite数据库中大小为0,android,sqlite,listview,Android,Sqlite,Listview,我试着增加价值 来自sqlite数据库的列表视图 当我在数据库中添加值时 这是错误的 android.database.CursorIndexOutOfBoundsException: 已请求索引0,大小为0 我还设置了检查相同值的条件 这是我的DatabaseHelper类 如何解决此错误 DatabaseHelper.java public class DatabaseHelper extends SQLiteOpenHelper { Context context; private s

我试着增加价值 来自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;
}
}

您的支票不起作用:

if (cursor.moveToFirst())
    cursor.moveToFirst();
...
如果不满足该条件,则在无条件读取光标的第二次冗余移动到第一次之后,代码仍将继续。改用类似的方式:

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

我更改了代码,但如果值不相同@laalth,则返回的消息始终已存在。导致此异常的问题在您的getNote中,而不是在您烤该消息的insertNote中;如果foo{curly\u block中的语句;}对不起,不要用勺子喂食;你也需要自己做一些工作。问题和解决方案已经在这个答案中。@laalto不应该是填鸭式的和填鸭式的:我遵循以下链接:在您的活动中初始化数据库Databasehelper db=new Databasehelper Context;插入呼叫:db。插入新的注释、地址;获取调用:db.getAllNotes;好啊我会给你所有的班级档案
if (cursor.moveToFirst()) {
    ...
}
***// 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;
    }
}