2016-04-15 32 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