简体   繁体   中英

Error while fetching data from SQlite in Android Studio

I have a JSON Array. I'm using DatabaseHelper to transfer the data but I'm not able to fetch the data. I know I'm making a simple mistake but it's just not visible.

This is the onCreate method

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

listView.setAdapter(adapter);

And this is the getAllData

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

This is something I did once. Can you get an idea from this? :)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM