在Android Studio中从SQlite获取数据时出错

时间:2016-04-15 16:07:44

标签: java android sqlite

我有一个JSON数组。我正在使用DatabaseHelper来传输数据,但我无法获取数据。我知道我犯了一个简单的错误,但它只是不可见。

这是onCreate方法

arrayList = database.getAllData();
ArrayAdapter adapter = new ArrayAdapter(getApplicationContext(),
        android.R.layout.activity_list_item,
        android.R.id.text1,
        arrayList);

listView.setAdapter(adapter);

这是getAllData

public Cursor getAllData() {
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor res = db.rawQuery("select * from "+TABLE_NAME,null);
    return res;
}

1 个答案:

答案 0 :(得分:0)

这是我做过的一次。你能从中得到一个想法吗? :)

public ArrayList<Item_Record> getAllRecords_ArrayList (String Table_Name) {

  // Create an array list
  ArrayList<Item_Record> List_Of_Records = new ArrayList<>();
  // Create a database object
  SQLiteDatabase DB = this.getReadableDatabase();

  // Create a cursor file we get from executing this above command
  Cursor crsr = DB.query(
        Table_Name,
        new String[] {COLUMN_DATE, COLUMN_CATEGORY, COLUMN_AMOUNT},
        null, null, null, null, COLUMN_DATE);

  crsr.moveToFirst();

  while (! crsr.isAfterLast()) {

     // Add that to the array list
     List_Of_Records.add(new Item_Record(
           crsr.getString(crsr.getColumnIndex(COLUMN_DATE)),
           crsr.getString(crsr.getColumnIndex(COLUMN_CATEGORY)),
           crsr.getDouble(crsr.getColumnIndex(COLUMN_AMOUNT))));

     // Go to the next row
     crsr.moveToNext();
  }
  // Closes database and cursor and return the list
  crsr.close(); DB.close();
  return List_Of_Records;
}