Q:

Android - How to add all the values in a database to arraylist

I have the following database class, I have a simple question... getrecords() method returns just the first name in the database i.e., 10 , All i need is that I want to store all the names in the database to the arraylist i.e., 10 and 100, how do i do that?. I am a beginner , so don't have much knowledge about this stuff. Thankyou for your help !
public class Database {

     public static final String KEY_ROWID = "ID";
        public static final String KEY_NAME = "NAME";
        public static final String KEY_HOTNESS = "PLACE";

    private static final String DATABASE_NAME = "Database.db";
    private static final String DATABASE_TABLE = "peopleTable";
    private static final int DATABASE_VERSION = 6;

    private static DbHelper ourHelper;
    private final Context ourContext;
    private static SQLiteDatabase ourDatabase;

    private static class DbHelper extends SQLiteOpenHelper{

        public DbHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
            // TODO Auto-generated constructor stub
        }

        @Override //I create it here, right?
        public void onCreate(SQLiteDatabase db) {

             db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" +
                     KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                     KEY_NAME + " TEXT NOT NULL UNIQUE, " +
                     KEY_HOTNESS + " TEXT NOT NULL);"                    
         );                 

            db.execSQL("INSERT INTO " + getDatabaseTable() + "(NAME, PLACE) VALUES('10',' Plantation')");
            db.execSQL("INSERT INTO " + getDatabaseTable() + "(NAME, PLACE) VALUES('100','NR Plantation')");

        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            db.execSQL("DROP TABLE IF EXISTS " + getDatabaseTable());
            onCreate(db);
        }

        public void close(Database database) {
            // TODO Auto-generated method stub

        }
    }

    public Database(Context c){
        ourContext = c;
    }

    public Database open() throws SQLException{
        ourHelper = new DbHelper(ourContext);
        ourDatabase = ourHelper.getWritableDatabase();
        return this;        
    }

    public void close() {

    ourHelper.close(); 
}

    public static String getDatabaseTable() {
        return DATABASE_TABLE;
    }

    public List<String> getrecords() {
        // TODO Auto-generated method stub
        List<String> arraylist = new ArrayList<String>();
        String[] columns = new String[]{  KEY_NAME };
        Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
        String result = "";

        int iName = c.getColumnIndex(KEY_NAME);

        for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
            arraylist.add(c.getString(iName));

            return arraylist;

        }

        return arraylist;
    }

}

A:

Your method should look like this :

public ArrayList<String> getRecords(){
    ArrayList<String> data=new ArrayList<String>();
    Cursor cursor = db.query(DATABASE_TABLE, new String[]{"column names"},null, null, null, null, null);
    String fieldToAdd=null;
    while(cursor.moveToNext()){
       fieldToAdd=cursor.getString(0);
       data.add(fieldToAdd);
    }
    cursor.close();  // dont forget to close the cursor after operation done
    return data;
}

this method will return all field values from database to arraylist

