我试图从a中获取所选项目字符串Spinner.到目前为止,我已经得到了这个:
bundle.putString(ListDbAdapter.DB_PRI, v.getText().toString());
Run Code Online (Sandbox Code Playgroud)
这不起作用,并给出一个类转换异常(我以为我可以View转换为继承它的小部件.显然不是!)那么如何获得a的选定值Spinner?
是否有提示类似于为编辑文本字段提供的提示的微调器.我知道您可以使用一个提示标题栏的提示,但仍然将初始微调器字段留空,直到您单击进入微调器.我目前有一种粗略的方法是将一个虚拟字段设置为微调器数组的第一部分,这是问题,然后在末尾检查以确保微调器不等于问题字符串.有没有更干净/更好的方法呢?
谢谢!
我在数据库中有1-50条记录.我使用游标获取这些数据,并使用简单光标适配器将这些值设置为Spinner.现在我需要的是我想设置一个值,将第39个值设置为默认值.但不是我的立场,我想通过它的价值来设定.
我知道如何通过其位置设置微调器默认值
spinner.setSelection(39)
Run Code Online (Sandbox Code Playgroud)
将微调器设置为该值.
但我不知道在数据库中通过其值(文本)设置微调器默认值.我知道数据库中的值.例如,"书籍"是微调器中的一个值.我需要将微调器默认设置为书籍.
有没有办法做到这一点?
我已经读过几件关于它的事情,但我找不到我需要的东西.我想保留灰色箭头,但我想从默认样式中删除水平条并具有白色背景.你知道我怎么做吗?
这是我现在拥有的(默认微调器样式):

这就是我想要的:

我在Spinner上遇到setSelection问题.我在代码中显示微调器时将值设置为预选,但它没有任何效果,并且始终选择列表中的第一个选项.代码如下所示:
LayoutInflater li = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View dialogView = li.inflate(R.layout.edit_event, null);
...
ArrayList<String> routes = new ArrayList<String>();
// routes filled with values at runtime
...
ArrayAdapter<String> aa = new ArrayAdapter<String>(GOFdroid.this, android.R.layout.simple_spinner_item, routes);
aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Spinner destSpinner = (Spinner) dialogView.findViewById(R.id.edit_event_destination);
String dest = events.get(pos).getDestination();
int routesPos = routes.indexOf(dest);
Log.d(TAG, "Dest: " + dest + ", pos: " + routesPos);
destSpinner.setSelection(routesPos);
destSpinner.setAdapter(aa);
Run Code Online (Sandbox Code Playgroud)
除了setSelection-part之外,代码按预期工作,我无法弄清楚原因.
微调器的XML布局看起来像这样(不是整个布局,只有微调器部分):
// DESTINATION
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Destination:" />
<Spinner
android:id="@+id/edit_event_destination"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:prompt="@string/choose_dest"
android:layout_marginBottom="10dip"
android:text="" />
Run Code Online (Sandbox Code Playgroud)
非常感谢帮助!
莱纳斯
我以下面的方式声明我的Spinner(它非常静态,所以我有2个字符串数组array.xml用于标题和值)
<Spinner
android:id="@+id/searchCriteria"
android:entries="@array/searchBy"
android:entryValues="@array/searchByValues" />
Run Code Online (Sandbox Code Playgroud)
我希望spinner.getSelectedItem()返回一个数组,[title, value]
但事实上它只返回一个标题字符串.它无视
android:entryValues吗?我如何获得价值,而不是它的标题?这仅适用于XML还是我需要创建适配器并以编程方式执行?
你如何创建setError()(类似于a TextView/EditText)函数Spinner?以下不起作用:
我尝试在构造函数中扩展Spinner类:
ArrayAdapter<String> aa = new ArrayAdapter<String>(getContext(),
android.R.layout.simple_spinner_item, android.R.id.text1,
items);
aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
setAdapter(aa);
tv = (TextView) findViewById(android.R.id.text1);
// types_layout_list_tv
ctv = (CheckedTextView) aa.getDropDownView(1, null, null);
tv2 = (TextView) aa.getView(1, null, null);
Run Code Online (Sandbox Code Playgroud)
setError 方法:
public void setError(String str) {
if (tv != null)
tv.setError(str);
if(tv2!=null)
tv2.setError(str);
if (ctv != null)
ctv.setError(str);
}
Run Code Online (Sandbox Code Playgroud) 我有一个OnItemSelectedListenerfor my Spinner,但是当所选项目与前一个项目相同时,它不会被调用.显然,OnClickListener这不是一个选项Spinner.每次用户点击某个项目时我都需要抓住.任何的想法?
也许这Spinner是ActionBar扰乱正常行为的事实?
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.tracklist_menu, menu);
Spinner spinner = (Spinner) menu.findItem(R.id.option_ordering_spinner)
.getActionView();
spinner.setAdapter(mSpinnerAdapter);
spinner.setSelection(PrefsHelper.getOrderingSpinnerPos(prefs));
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
String str = "selected";
System.out.println(str);
if (optionMenuInitialized) {
switch (position) {
case 0:
// rdm
getActivity()
.sendBroadcast(
new Intent(
MyIntentAction.DO_RESHUFFLE_PLAYLIST));
smp.setCurrentTracklistCursorPos(-1);
trackAdapter.notifyDataSetChanged();
break;
case 1:
// artist
getActivity()
.sendBroadcast(
new Intent(
MyIntentAction.DO_ORDER_PLAYLIST_BY_ARTIST)); …Run Code Online (Sandbox Code Playgroud) android listener spinner actionbarsherlock android-actionbar
我正在为我的应用程序使用appcompat主题.需要知道我如何显示下划线到微调器.它只是显示锚.我尝试使用android:background设置下划线但它会使锚点消失.
我试图在微调框小部件中添加一个提示,因为没有提示的选项EditText,我希望将性别显示为提示,单击时它必须只显示男性和女性而不是提示.
如何使用XML完成它
微调器的XML代码.
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/spinner1"
android:entries="@array/gender"
android:layout_marginTop="10dp"
android:layout_marginLeft="25dp"
android:layout_marginRight="25dp"
android:layout_gravity="center_horizontal" />
Run Code Online (Sandbox Code Playgroud)
String微调器的数组
<string-array name="gender">
<item>Male</item>
<item>Female</item>
</string-array>
Run Code Online (Sandbox Code Playgroud)