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"?


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

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) 
_The Busy Coder's Guide to Android Development_ Version 2.0 Published!


Sponsored Links

Other Threads

1. how-to:Android SDK sources in eclipse

I've made a very short step-by-step blog post on how to get to see the
android sources in eclipse.

hope it helps


2. Facebook and twitter integration


I want to integrate facebook and twitter into my android app.  Everwhere I
found the code of showing the dialog box of facebook and twitter login and
then posting the content from there.
What I want to do is, on twitter or facebook button click it should forward
to twitter site for e.g. and my article id should get
displayed in the tweet box (twitter) or walll post box (facebook) in the
site directly.
Just like it has done in ndtv app.
Please do help in this.

Thanks & Regards,
Laxmi Verma


3. programming a simple form application

4. peer to peer streaming protocol

5. Excessive JNI global references (2001)

6. PIP for android video?

7. Emulator doesn't shut down