Getting recent stations I made a database for my Android app using SQLite and it works fine. However, as I understood, SQLite calls shouldn't be made on UI thread.

Is the below okay or not?

/**
     * @param howMany Amount of Stations returned, passing null returns all.
     *                Sorted after last time searched.
     * @return
     */
    public ArrayList&lt;Station&gt; getRecentStations(final int howMany) {
        final ArrayList&lt;Station&gt; stations = new ArrayList&lt;Station&gt;(howMany);

        new AsyncTask&lt;Void, Void, Cursor&gt;() {

            @Override
            protected Cursor doInBackground(Void... params) {
                SQLiteDatabase db = getReadableDatabase();

                Cursor cursor = db.query(TABLE_NAME_RECENT, COLUMNS_RECENT, null, null, null, null, COLUMN_TIME_SEARCHED + " DESC", Integer.toString(howMany));

            if (cursor != null) {
                    cursor.moveToFirst();
                }
                while (!cursor.isAfterLast()) {

                    Station s = new Station();
                    s.setStationId(cursor.getInt(0));
                    s.setStationName(cursor.getString(1));
                    s.setLatitude(Double.parseDouble(cursor.getString(2)));
                    s.setLongitude(Double.parseDouble(cursor.getString(3)));
                    s.setType(cursor.getString(4));
                    s.setTimeSearched(cursor.getString(5));
                    cursor.moveToNext();
                    stations.add(s);


                }

                db.close();
            }


        }.execute();
        return stations;

    }
However as I understood SQLite calls shouldn't be made on UI thread.


To be honest I wasn't aware of this: I make SQLite calls on the UI thread in many apps.
I think it's fine, unless you expect the calls might be slow.
But since you already got it working with AsyncTask,
then you can just as well leave as it is.

I have some other recommendations though.

Change the method to return List&lt;Station&gt; instead of ArrayList&lt;Station&gt;. You should try to use interface types (List&lt;&gt;) whenever possible instead of implementations (ArrayList&lt;&gt;).

A simpler way to iterate over the cursor:

while (cursor.moveToNext()) {
    Station s = new Station();
    s.setStationId(cursor.getInt(0));
    s.setStationName(cursor.getString(1));
    s.setLatitude(Double.parseDouble(cursor.getString(2)));
    s.setLongitude(Double.parseDouble(cursor.getString(3)));
    s.setType(cursor.getString(4));
    s.setTimeSearched(cursor.getString(5));
    stations.add(s);
}


But actually... Why does doInBackground return a Cursor? Why not do everything in this method and do nothing in onPostExecute? It seems that would make a lot more sense.