2016-04-15 29 views
0

我有一個JSON數組。我正在使用DatabaseHelper傳輸數據,但我無法獲取數據。我知道我犯了一個簡單的錯誤,但它只是不可見。在Android Studio中從SQlite獲取數據時出錯

這是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; 
} 
+0

是不是this.getReadableDatabase(); ? – Blogger

+0

@Venky您使用本地數據庫的第一次獲取數據,一次使用後臺方法。 –

+2

並且您尋找arraylist並且getAllData()方法返回一個遊標! – Blogger

回答

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

我可以嘗試,謝謝雖然 – Venky