I have an SQLite Database.
Here's some of the code setting it up:
// Field Names:
public static final String KEY_ROWID = "_id";
public static final String KEY_NAME = "name";
public static final String KEY_DESCRIPTION = "description";
public static final String[] ALL_KEYS = new String[] {KEY_ROWID, KEY_NAME, KEY_DESCRIPTION};
// Column Numbers for each Field Name:
public static final int COL_ROWID = 0;
public static final int COL_NAME = 1;
public static final int COL_DESCRIPTION = 2;
// DataBase info:
public static final String DATABASE_NAME = "dbMetrics";
public static final String DATABASE_TABLE = "mainMetrics";
public static final int DATABASE_VERSION = 1;
//SQL statement to create database
private static final String DATABASE_CREATE_SQL =
"CREATE TABLE " + DATABASE_TABLE
+ " (" + KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ KEY_NAME + " TEXT NOT NULL, "
+ KEY_DESCRIPTION + " TEXT"
+ ");";
I want to output one of my columns KEY_NAME
as an ArrayList.
To do this, I have so far generated the following code:
public ArrayList<String> getAllStringValues() {
ArrayList<String> myStringValues = new ArrayList<String>();
Cursor result = db.query(true, DATABASE_TABLE,
new String[] {KEY_NAME}, null, null, null, null,
null, null);
**********CODE NEEDED HERE***********
System.out.println(Arrays.toString(myStringValues.toArray()));
return myStringValues ;
}
However, I am not sure how I get the data from the column into an ArrayList from here.
My Question
Can someone give me some assistance as to how to design a loop that will go through each row in the column and put that data in an ArrayList?
I'm pretty sure I will need to at some point use
result.getString(result.getColumnIndex(KEY_NAME))
in order to do this, but again, I'm not sure how.
Any help would be greatly appreciated.