Android Spinner 所选项目
我正在从数据库中填充这样的微调器
// Populating the City Spinner
Cursor cities = db.cityList();
startManagingCursor(cities);
// create an array to specify which fields we want to display
String[] from = new String[] { DBAdapter.KEY_NAME };
// create an array of the display item we want to bind our data to
int[] to = new int[] { android.R.id.text1 };
Spinner cityList = (Spinner) this.findViewById(R.id.citySpiner);
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, cities, from, to);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
cityList.setAdapter(adapter);
当我尝试从这样的微调器的选定项目中获取内容时,
// Get the City
Spinner getCity = (Spinner) findViewById(R.id.citySpiner);
String cityName = getCity.getSelectedItem().toString();
我得到以下内容。 有没有办法可以从数据库中获取城市名称或城市 ID。
I am populating a spinner from the database like this
// Populating the City Spinner
Cursor cities = db.cityList();
startManagingCursor(cities);
// create an array to specify which fields we want to display
String[] from = new String[] { DBAdapter.KEY_NAME };
// create an array of the display item we want to bind our data to
int[] to = new int[] { android.R.id.text1 };
Spinner cityList = (Spinner) this.findViewById(R.id.citySpiner);
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, cities, from, to);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
cityList.setAdapter(adapter);
When i try to get the content from the selected item of the spinner like this
// Get the City
Spinner getCity = (Spinner) findViewById(R.id.citySpiner);
String cityName = getCity.getSelectedItem().toString();
i get the following.
Is there a way i can get the city name or the city id from the database.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我认为,当您使用自定义适配器并在适配器中给出三个列表时...
您无法仅通过调用 getSelectedItem() 来获取所选文本...
使用这个:
希望它有帮助...
I think, as you are using a customadapter and giving three lists in adapter...
you can't get the selected text simply by calling the getSelectedItem()..
Use this:
Hope it helps....
只需从微调器中获取适配器并从光标中获取字符串
Just get the adapter from your spinner and get the string from the cursor
我实现它的方式是这样的:
您必须将 getItem() 方法转换为游标,因为它本质上只是返回游标而不是游标中的单个行,这很烦人。
将光标移动到第一行
现在,您可以使用与设置旋转器适配器时最初查询的表相匹配的列名从光标获取字符串,该适配器是使用 getAdapter() 在上面抓取的,
希望这有帮助,并感谢任何反馈:)
The way I achieved it was like this:
You will have to cast the getItem() method to a cursor as it is essentially just returning a cursor rather than an individual row in the cursor which is annoying.
Move the cursor to the first row
Now you can get the string from the cursor using the column name that matches table originally queried when setting your spinners adapter which was grabbed above using getAdapter()
Hope this helps and would appreciate any feedback :)
希望这有帮助!
Hope this helps!!