Java SQLite,以数组形式返回数据

Java SQLite,以数组形式返回数据,java,android,arrays,sqlite,Java,Android,Arrays,Sqlite,我的android应用程序中有一个SQLite数据库,其结构如下: public void onCreate(SQLiteDatabase db) { String CREATE_LISTS_TABLE = "CREATE TABLE " + TABLE_LISTS + "("+ _ID + " INTEGER PRIMARY KEY , " +

我的android应用程序中有一个SQLite数据库,其结构如下:

public void onCreate(SQLiteDatabase db) {
  String CREATE_LISTS_TABLE = "CREATE TABLE " + TABLE_LISTS +
                              "("+
                              _ID + " INTEGER PRIMARY KEY , " +
                              NOTE + " TEXT" +
                              ")";
  db.execSQL(CREATE_LISTS_TABLE);
}
这是可行的,因为我可以毫无问题地将数据插入其中。但是,我需要将注释存储在一个数组中。我目前有以下疑问:

public List<String> getAllNotes() {
  List<String> notes = new ArrayList<>();

  String GET_ALL_NOTES = "SELECT * FROM " + TABLE_LISTS;

  SQLiteDatabase db = getReadableDatabase();
  if(db!=null)
  {
     Cursor cursor = db.rawQuery(GET_ALL_NOTES, null);
     cursor.moveToFirst();
     while(!cursor.isAfterLast())
     {
       notes.add(String.valueOf(cursor.getInt(cursor.getColumnIndex("notes"))));
       cursor.moveToNext();
     }
     cursor.close();
  }
  db.close();

  return notes;
}
我想知道如何解决这个问题,我已经阅读了android开发者的资料,但我似乎什么都做不到

提前感谢

检查“NOTE”的值,并将其用于:
notes.add(String.valueOf(cursor.getInt(cursor.getColumnIndex(NOTE)))

我认为打电话最好的方式应该是这样:

// Check the cursor
    if(cursor != null) {
        if (cursor.moveToFirst()) {
            // Variables to be used
            String note;

            // Col position
            int colNote = cursor.getColumnIndex(NOTE);

            do {
                // Get the information
                note = cursor.getString(colNote);

                // Add the note
                notes.add(note);
            } while (cursor.moveToNext());
        }

        // Close the cursor
        cursor.close();
    }
检查“NOTE”的值,并将其用于:
notes.add(String.valueOf(cursor.getInt(cursor.getColumnIndex(NOTE)))

我认为打电话最好的方式应该是这样:

// Check the cursor
    if(cursor != null) {
        if (cursor.moveToFirst()) {
            // Variables to be used
            String note;

            // Col position
            int colNote = cursor.getColumnIndex(NOTE);

            do {
                // Get the information
                note = cursor.getString(colNote);

                // Add the note
                notes.add(note);
            } while (cursor.moveToNext());
        }

        // Close the cursor
        cursor.close();
    }

因为您只从数据库中获取整数和字符串,所以可以尝试使用HashMap,而不是使用ArrayList。因此,只需给出键就可以得到值。下面的简单代码也适用于ArrayList,只需稍作修改

试试这个

  HashMap<Integer,String> notes = new HashMap<Integer,String>() ;

        Cursor cursor = db.rawQuery(GET_ALL_NOTES, null);

        while (cursor.moveToNext())

        {
            int i = cursor.getInt(0);
            String s = cursor.getString(1);
            notes.put (i,s) ;
        }

        cursor.close();
HashMap notes=newhashmap();
Cursor Cursor=db.rawQuery(获取所有注释,null);
while(cursor.moveToNext())
{
inti=cursor.getInt(0);
字符串s=cursor.getString(1);
(i,s);
}
cursor.close();

因为您只从数据库中获取整数和字符串,而不是使用ArrayList,所以可以尝试使用HashMap。因此,只需给出键就可以得到值。下面的简单代码也适用于ArrayList,只需稍作修改

试试这个

  HashMap<Integer,String> notes = new HashMap<Integer,String>() ;

        Cursor cursor = db.rawQuery(GET_ALL_NOTES, null);

        while (cursor.moveToNext())

        {
            int i = cursor.getInt(0);
            String s = cursor.getString(1);
            notes.put (i,s) ;
        }

        cursor.close();
HashMap notes=newhashmap();
Cursor Cursor=db.rawQuery(获取所有注释,null);
while(cursor.moveToNext())
{
inti=cursor.getInt(0);
字符串s=cursor.getString(1);
(i,s);
}
cursor.close();

col-1
表示您的
光标中没有
“注释”
列。当然不仅仅是“注意”
?为什么不直接使用用来创建表的
NOTE
。有什么想法吗?如果您在该列中存储了
字符串
,为什么要使用
getInt()
来检索该值?使用
getString()
。我真是太感谢你了,伙计,我已经在这件事上纠缠了好几个小时了,这真是太愚蠢了。谢谢allot mate。这只是我想象中的纯文本,所以你所需要的就是
注释。添加(cursor.getString(cursor.getColumnIndex(“NOTE”);
col-1
意味着
光标中没有
“notes”
列。当然不仅仅是
“NOTE”
?为什么不直接使用用于创建表的
注释呢?是的,我很笨,但是它似乎只显示0,而不是存储的字符串。有什么想法吗?如果您在该列中存储了
字符串
,为什么要使用
getInt()
来检索值?使用
getString()
。我真是太感谢你了,伙计,我已经在这件事上纠缠了好几个小时了,这是一件很愚蠢的事。谢谢allot mate。这只是我想象中的纯文本,所以你所需要的就是
notes.add(cursor.getString(cursor.getColumnIndex(“NOTE”);