I am using a spinnerbox in my application. The spinnerbox is to be filled with projects from the database. This itself already works. However i need one extra item in the drop down list. I want the first item to be "general" general is not a project. Thus it is not retrieved from the database. Is there someway to either inject it in thye cursor or adapter?
4 Answers
What worked for me was to do a UNION in the sql query.
dbStatic.rawQuery(
" SELECT 2 as deftop, typeid as _id, typename as label FROM objtypes UNION "+
" SELECT 1 as deftop, -1 as _id, "+strDefaultSpinner+" as label "+
" ORDER BY deftop asc, label ", null
);
if the item selected is -1, then it's the default value. Otherwise it's a record from the table.

- 746
- 1
- 7
- 17
I encountered the same problem a while ago .. the problem is that you cant actually insert information into a cursor (because its just a pointer) so I believe you have to have some kind of mediator in between .. my way of solving it was to simply crate a string array [cursor.getCount+1] then insert your "general" in [0] and then go through your cursor to insert the rest ..
it does go through the items an extra round (which isn't so bad in my case) but for a long list you might want to override the adaptar and insert a line before it goes through the cursor which i cannot help you with the code for that..

- 2,252
- 1
- 22
- 32
-
Hey joe. Thanks for your response. I tried doing that. But it is of absolute importance that i can get the id, name, etc out of the selected item if its not general. A project name can occure twice. Ill save u the logics behind the descision. But thats how its supposed to go. – Joey Roosing Nov 21 '11 at 14:21
-
I see... well I got another idea but im not entirely sure its possible. maybe arrange your data in a MatrixCursor (merge your database cursor into it). im not sure if simpleCursorAdaptar will be enough because i cant think of where to store this extra information (other then in tags or invisible views) so maybe overriding the adaptar will also be necessary in this solution. let me know if you figure this out. – Joe Nov 21 '11 at 14:42
-
i solved this in a different way. Its so simple i completely didnt think of it before x.x – Joey Roosing Nov 21 '11 at 15:13
My example is working with androidx, room and spinner component.
In your content provider you should have something like.
@Nullable
@Override
public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection,
@Nullable String[] selectionArgs, @Nullable String sortOrder) {
...
String all = context.getResources().getString(R.string.search_spinner_all);
SimpleSQLiteQuery query = new SimpleSQLiteQuery("SELECT '"+all+"' as "+ThemeData.COLUMN_ID+",'' as "+ThemeData.COLUMN_SET_COUNT+",0 as "+ThemeData.COLUMN_SUBTHEME_COUNT
+",0 as "+ThemeData.COLUMN_YEAR_FROM+",0 as "+ThemeData.COLUMN_YEAR_TO
+" UNION SELECT * FROM " + ThemeData.TABLE_NAME+" ORDER BY "+ ThemeData.COLUMN_ID);
cursor = themeDataDao.selectAll(query);
...
}
In your dao, use
@RawQuery
@Dao
public interface ThemeDataDao {
@RawQuery
Cursor selectAll(SupportSQLiteQuery query);
}
You got it, you can use your simple implementation or cursor adapter !
themesAdapter = new SimpleCursorAdapter(getContext(), R.layout.spinner_with_count, null,
new String[]{ThemeData.COLUMN_ID, ThemeData.COLUMN_SET_COUNT}, new int[] { R.id.spinnerTxLabel, R.id.spinnerTxCount }, 0);
inputTheme.setAdapter(themesAdapter);
LoaderManager.getInstance(this).initLoader(LOADER_THEMES, null, themesLoaderCallback);

- 3,330
- 2
- 24
- 25
I managed to solve this in a differebt way then i originally planned. But it works well. Instead of a general option. I made a checkbox. Is it checked, then its general and the spinner is setunabled. And if unchecked it gets set to enabled. This works for my situation.

- 2,145
- 5
- 25
- 42