By Alvin Alexander. Last updated: June 4, 2016
There may be better ways to do this, but I just used this code to convert an Android CursorWrapper
object named QuoteCursor
into a Java List
, specifically an ArrayList
:
public List<String> getAllActiveQuotes() { List<String> quotes = new ArrayList<>(); QuotesDatabaseHelper.QuoteCursor quoteCursor = mHelper.queryQuotes(); int quotesColumn = quoteCursor.getColumnIndex(QuotesDatabaseHelper.COL_QUOTES_QUOTE); for(quoteCursor.moveToFirst(); !quoteCursor.isAfterLast(); quoteCursor.moveToNext()) { quotes.add(quoteCursor.getString(quotesColumn)); } return quotes; }
The quotesColumn
field isn’t important for this example. It’s just an int
that represents the column you’re extracting from your database table. In my case I’m filling my ArrayList
with the second column, but again, that’s all quotesColumn
is, an int
that specifies the desired column number.
If you needed to see how to convert an Android CursorWrapper
into a List/ArrayList, I hope this is helpful.