How to add extra elements into Cursor or SimpleCursorAdapter?

by Developer » Sun, 01 Feb 2009 08:08:34 GMT


Sponsored Links
 Suppose I get a cursor that contains all rows from my database, and I
want to show them on a spinner, the following code will work. But I
want to show two extra items as the spinner elements, how can I add
those two elements?

For example, if the following code can show 4 spinner elements
"Orange" "Apple" "Cabbage" "Green Onion", how can I add an element in
the spinner saying "All fruit" "All Vegetable"?

Thanks


class ShowElements extends Activity {
    .....
    private void populateSpinner() {
        Cursor cursor = mDbHelper.fetchAllExistingElements();  // get
all items
        startManagingCursor(cursor);
        String[] from = new String[]{ ShowElements.KEY_NAME };
        int[] to = new int[]{android.R.id.text1};
        SimpleCursorAdapter adapter = new SimpleCursorAdapter(
                        this, android.R.layout.simple_spinner_item, cursor, 
from,
to);
        adapter.setDropDownViewResource
(android.R.layout.simple_spinner_dropdown_item);
        mCardSpinner.setAdapter(adapter);
    }
}
--~--~---------~--~----~------------~-------~--~----~



How to add extra elements into Cursor or SimpleCursorAdapter?

by Mark Murphy » Sun, 01 Feb 2009 08:15:04 GMT


 


Here are some ideas...

Option #1: Put the extra two in the database.

Option #2: Don't pass the Cursor into SimpleCursorAdapter, but rather
iterate over the Cursor, put the results into an ArrayList or something,
and use that with an ArrayAdapter.

Option #3: Create a CursorWrapper that injects your two extra values
into the results.

-- 
Mark Murphy (a Commons Guy)
 http://commonsware.com 
_The Busy Coder's Guide to Android Development_ Version 2.0 Published!

--~--~---------~--~----~------------~-------~--~----~


Sponsored Links