Search code examples
androidsqlitegoogle-mapslocationandroid-cursorloader

Pull SQLite data to an array list


So I have been trying to use a cursorLoader but I can't seem to get around it. I have a database and I have my class set up. I have data stored in the SQLite database. I now want to be able to get all the data from that row (called location) and I want to store it in an array list so I can use it. The code for the CursorLoader is as follows :

@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {

    markerPoints = new ArrayList<LatLng>();

    switch (id) {
        case NOTES_LOADER: {
            return new CursorLoader(
                    this,                                           // Parent activity context
                    NotesProvider.CONTENT_URI,                      // Table to query
                    null,                                           // Projection to return
                    DBOpenHelper.NOTE_DATE + " = " + dateString,    // No selection clause
                    null,                                           // No selection arguments
                    null                                            // Default sort order
            );
        }

        case NOTES_LOCATION: {
            return new CursorLoader(
                    this,
                    NotesProvider.CONTENT_URI,
                    null,
                    DBOpenHelper.NOTE_LOCATION,
                    null,
                    null
            );
        }

        default: {
            // An invalid id was passed in
            return null;
        }
    }
}

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
}

@Override
public void onLoaderReset(Loader<Cursor> loader) {
}

The tricky part is that the locations I want to load have to be from a specific day (NOTE_DATE). So the array list can only be from e.g. 2016-02-17. Once the day is picked then the locations will be saved to the array and then I can continue on with them. Any ideas?


Solution

  • So I managed to fix this solution. The code I used was as follows :

    public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
    
        ArrayList<LatLng> points = new ArrayList<LatLng>();
        PolylineOptions lineOptions;
    
        try {
            int columnIndex = data.getColumnIndex(DBOpenHelper.NOTE_LOCATION);
            while (data.moveToNext()) {
                String latLongStr = data.getString(columnIndex);
                StringTokenizer tokens = new StringTokenizer(latLongStr, ",");
                String latString = tokens.nextToken();
                String longString = tokens.nextToken();
                double lat = Double.parseDouble(latString);
                double lng = Double.parseDouble(longString);
                points.add(new LatLng(lat, lng));
            }
    

    So I had to pass the array list into the finished cursor and then seperate it using a Tokenizer.