android spinner default value using cursor adapter

你离开我真会死。 提交于 2019-12-11 10:33:22

问题


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?


回答1:


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.




回答2:


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..




回答3:


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.



来源:https://stackoverflow.com/questions/8212707/android-spinner-default-value-using-cursor-adapter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!