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


Other Threads

1. Java App UI / Engine C split query

If I have a large existing component alredy developed for another
platform in C and I want to port it to Android what are the options?

If I re-write the entire thing in Java that will take a long time and
also I would be worried about performance issues with everything
running in the JVM.

So if I split it so that the UI is in Java and the engine is in C what
are the available mechanism(s) for making the C API available to the
Java application?
How would I debug this i.e. if I run the Java app in Eclipse how can I
step into the C code?

Are there other options?

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

2. Android Email Notification

Having read several times the topic on Push Notification Techniques
bellow, I still wonder how the current email notification technique on
android works?!

3. Exchanging Messages between services containing Bundles with self-developed Parcelables

4. Music App Weirdness

5. Exchanging Messages between services containing Bundles with self developed Parcelables

6. how to start the default music application from my own application

7. I'm Trying To Learn.......