Android Spinner 不显示选定的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20354380/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spinner does not show selected value
提问by Jolly
I have implemented the spinner by populating the array list through database.I can get and show the array list in my spinner array adapter but if I select the item in spinner it does not shown in spinner?What I had mistake here?
我已经通过数据库填充数组列表来实现微调器。我可以在微调器数组适配器中获取并显示数组列表,但是如果我在微调器中选择项目,它不会在微调器中显示?我在这里犯了什么错误?
Here is my code,
这是我的代码,
Spinner spinner1 = (Spinner) findViewById(R.id.prospin);
ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, providerlist);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(adapter1);
I get the selected item string by using this,
我通过使用这个获得选定的项目字符串,
Spinner provid = (Spinner)findViewById(R.id.prospin);
String provider =provid.getSelectedItem().toString();
Can anyone help me out pls!!!
任何人都可以帮助我请!!!!
回答by Catluc
This answer may be a little stupid but try it. It worked for me.
这个答案可能有点愚蠢,但试试吧。它对我有用。
- Check the background color of your spinner!
- And if it`s white change it
- Enjoy it!
- 检查微调器的背景颜色!
- 如果它是白色的改变它
- 好好享受!
回答by Pratik Butani
I got same problem and solved by adding notifyDataSetChanged()
after binding data in Spinner.
我遇到了同样的问题,并通过notifyDataSetChanged()
在 Spinner 中添加后绑定数据来解决。
First of all I have bind adapter with BlankArrayList
then getting List of Items from Server and added to that List but forgot to notifyDataSetChanged()
after updated List.
首先,我将适配器与空白绑定,ArrayList
然后从服务器获取项目列表并添加到该列表但notifyDataSetChanged()
在更新列表后忘记了。
just add adapter.notifyDataSetChanged();
after updating list.
只需adapter.notifyDataSetChanged();
在更新列表后添加 。
Hope it will helpful.
希望它会有所帮助。
回答by TastyCatFood
Problem:
问题:
Spinner displays neither the default nor selected item value. But drop-down menu items are show when selected.
Spinner 既不显示默认值也不显示所选项目值。但是下拉菜单项会在选中时显示。
Cause:
原因:
Background and text color both being white!!!
背景和文字颜色均为白色!!!
Solutions:
解决方案:
xml(Preferable):
xml(首选):
Write a custom layout for a spiner item and use it instead of the default,android.R.layout.simple_spinner_item
.
为微调项编写自定义布局并使用它而不是默认的android.R.layout.simple_spinner_item
.
How to change spinner text size and text color?
Code(Less reliable):
代码(不太可靠):
your_spinner_instance.setOnItemSelectedListener(new Spinner.OnItemSelectedListener(){
public void onItemSelected(AdapterView<?> parent, View view, int pos,
long id) {
((TextView) view).setTextColor(Color.RED);
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
Android needs some major update or maybe dart and flutter should take over...
Android 需要一些重大更新,或者 dart 和 flutter 应该接管......
Thanks Catluc
谢谢卡特卢克
回答by Mladen Rakonjac
Use wrap_content
for the height of Spinner
.
使用wrap_content
的高度Spinner
。
Probably it does not have enough height to show text.
可能它没有足够的高度来显示文本。
回答by Vyshak Athreya
Well this also happens if the context is not properly given. I was using getApplicationContext()
where as it needs getBaseContext()
.
如果上下文没有正确给出,也会发生这种情况。我在getApplicationContext()
需要的地方使用getBaseContext()
。
回答by Ali Salesi
if you have a custom adapter you should change the TextView text color
如果您有自定义适配器,则应更改 TextView 文本颜色
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView view = (TextView) super.getView(position, convertView, parent);
view.setTextColor(Color.parseColor("#000000"));
return view;
}
and if you don't have a custom adapter you should just change spinner background
如果您没有自定义适配器,您应该只更改微调器背景
回答by JAMES MILLER
I just had this problem and after trying all of the solutions listed found out that the issue was that I had set the spinner layout_width
to 60dp.
我刚刚遇到了这个问题,在尝试了列出的所有解决方案后,我发现问题是我将微调器设置layout_width
为60dp。
Changed it to fill_parent and problem solved.
将其更改为 fill_parent 并解决了问题。
回答by Noby Ali
In my case the list is populated from firestore before supplied to the adapter construction. The list is already populated and is displayed in spinner. but the size of the list was returned zero. So I only can select the item from the list, but there is no display in the spinner text and unable to call the onitemselected part. I previously made a callback of the querysnapshot and from the snapshot I populated the list. it caused only adding the items to the spinner but cannot be selected. I resolved this by taking a callback of the list instead of the querysnapshot from a repository class and after this my list is populated well and its size is correct and the spinner works fine..my code is attached here.. hope this helps someone...
在我的情况下,列表在提供给适配器构造之前从 firestore 填充。该列表已填充并显示在微调器中。但列表的大小返回零。所以我只能从列表中选择项目,但是微调文本中没有显示并且无法调用onitemselected部分。我之前对查询快照进行了回调,并从快照中填充了列表。它导致仅将项目添加到微调器但无法选择。我通过从存储库类获取列表的回调而不是查询快照来解决此问题,此后我的列表填充良好并且其大小正确并且微调器工作正常..我的代码附在此处..希望这对某人有所帮助。 ..
//interface
public interface SprRbaCallback{
void onSprRbaCallback(List<String> list);
}
//Repository class
public void getRBA(final SprRbaCallback sprRbaCallback){
rbaCollection.get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
@Override
public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
for(DocumentSnapshot snapshot : queryDocumentSnapshots){
String item = (String) snapshot.get("dealerName");
assert item != null;
Log.d(TAG, item);
list.add(item);
}
Log.d(TAG, String.valueOf(list.size()));
sprRbaCallback.onSprRbaCallback(list);
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.d(TAG, e.toString());
}
});
}
// calling method..
private void addItemsToRBASpinner() {
firebaseRepository.getRBA(new FirebaseRepository.SprRbaCallback() {
@Override
public void onSprRbaCallback(List<String> list) {
int size = list.size();
Log.d(TAG + "listsize", String.valueOf(size));
ArrayAdapter<String> sprAdaptSelectRBA = new ArrayAdapter<String>(CreateAccountActivity.this,
R.layout.spinneritem, list);
sprAdaptSelectRBA.setDropDownViewResource(R.layout.rbaspinner_dropdown);
spr_rbaSelect.setAdapter(sprAdaptSelectRBA);
sprAdaptSelectRBA.notifyDataSetChanged();
spr_rbaSelect.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Log.d("NOBY", "ENTERED ON ITEMSELETED");
txt_selectedRBA = (String) parent.getItemAtPosition(position);
Toast.makeText(parent.getContext(), "Selected RBA: " + txt_selectedRBA, Toast.LENGTH_LONG).show();
Log.d("NOBY", txt_selectedRBA);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
Log.d("NOBY", "ENTERED NOTHING SELECTED");
txt_selectedRBA = "Dixon";
}
});
}
});
}
This code works fine for me...hope this help someone...:)
这段代码对我来说很好用...希望这能帮助别人...:)
回答by wafaa hilmy
add background color white for example and details will display for example
例如,添加背景颜色为白色,例如将显示详细信息
<Spinner
android:id="@+id/size_spinner"
style="@style/FormTextStyle"
android:drawSelectorOnTop="true"
android:layout_marginStart="@dimen/default_gap"
android:background="@android:color/white"
/>
回答by Kenart
This happens occasionally, I guess it's a bug but there's a workaround which is funny but just do it,
这种情况偶尔会发生,我想这是一个错误,但有一种解决方法很有趣,但就去做吧,
In the property bar of the spinner set the background to a different color,
在微调器的属性栏中将背景设置为不同的颜色,
In your .xml where you have your spinner, set:
在您拥有微调器的 .xml 中,设置:
android:background="@android:drawable/spinner_background_material"
run the code it will generate an error saying the background is private
运行代码它会产生一个错误,说背景是私有的
delete the line of the android:background
in the .xml and run the code again,your spinner should work fine showing all the values, if it doesn't take the extra step of setting background tint to a different color preferably black.
删除android:background
.xml 中的行并再次运行代码,您的微调器应该可以正常显示所有值,如果它没有采取额外的步骤将背景色调设置为不同的颜色,最好是黑色。
Run it again.
再次运行它。