Getting the SQLite database and storing it in an array of objects

I am watching an example Notes application from the Android SDK. I want to learn how to do this instead of using the CursorAdapter, just to go to the ListAdpater / ListView to figure it out, I want to know how I can handle the data myself; especially in the form of an ArrayList. In the example, the note mainly has an identifier, a title, and a body. I want to know how I can query the SQLite database and use the cursor that it returns to collect data and instantiate the objects of the object that I will call. A note that has parameters for id, title and body. Ultimately, I want to store all of these objects in an ArrayList for management. I'm just not sure how to deal with the Cursor. This is a pretty general question, but I just need someone to point me in the right direction.

+4
source share
3 answers

In fact, I realized this after the game. Therefore, I realized that using cursor.getColumnIndex("name_of_column") will return the index of the column that will be used in commands like cursor.getInt(cursor.getColumnIndex("_id")); . All I have to do is just use the for loop to view the entire list and use cursor.moveToNext() to just cursor.moveToNext() over the collected lines. I came up with these minutes after I posted this question. :)

+1
source

I can't ask your question, but do you need to query your db and then add the cursor data to an ArrayList?

  List<String> pointsList = new ArrayList<String>(); database = openOrCreateDatabase("favorites", SQLiteDatabase.OPEN_READWRITE, null); if(database!=null) { c= database.rawQuery(QUERY, null); c.moveToFirst(); while(c.isAfterLast()==false) { pointsList.add(c.getInt(c.getColumnIndex("id")); // do the same for other columns c.moveToNext(); } } database.close(); c.close(); 
+5
source

It can help you

  public ArrayList<ArrayList<Object>> getAllRowsAsArrays() { // create an ArrayList that will hold all the data collected from // the database. ArrayList<ArrayList<Object>> dataArrays = new ArrayList<ArrayList<Object>>(); // This is a database call that creates a "cursor" object. // The cursor object store the information collected from the // database and is used to iterate through the data. Cursor cursor; try { // ask the database object to create the cursor. cursor = db.query( TABLE_NAME, new String[]{TABLE_ROW_ID, TABLE_ROW_ONE, TABLE_ROW_TWO}, null, null, null, null, null ); // move the cursors pointer to position zero. cursor.moveToFirst(); // if there is data after the current cursor position, add it // to the ArrayList. if (!cursor.isAfterLast()) { do { ArrayList<Object> dataList = new ArrayList<Object>(); dataList.add(cursor.getLong(0)); dataList.add(cursor.getString(1)); dataList.add(cursor.getString(2)); dataArrays.add(dataList); } // move the cursor pointer up one position. while (cursor.moveToNext()); } } catch (SQLException e) { Log.e("DB Error", e.toString()); e.printStackTrace(); } // return the ArrayList that holds the data collected from // the database. return dataArrays; } 
0
source

All Articles