我使用翻新, 显示的项目,但我无法选择。 单击项目后,吐司和微调器什么都没有显示(我无法在微调器上选择项目)。......
<div class="main">
<section class="signup">
<div class="container">
<div class="row">
<div class="signup-content col-sm-12">
<form id="signup-form" class="signup-form" method="post" action="{% url 'login' %}">
{% csrf_token %}
<h2 class="form-title">Log In</h2>
{{ form | crispy}}
<input type="submit" style="margin-top: 10px" value="Login" class="button" id="" />
<input type="hidden" name="next" value="{{ next }}" />
</form>
<div class="col-sm-12" style="text-align: right; margin-top: 5px">
<a href="{% url 'password_reset' %}">Forgot Password?</a>
</div>
<p class="loginhere">
New Here ? <a href="/account/register" class="loginhere-link">Create an account here</a>
</p>
</div>
</div>
</div>
</section>
</div>
答案 0 :(得分:1)
当前解决方案:
您在setSpinner()
中为适配器设置了名为spinnerItem
的列表,该列表目前为空。然后在displayCities()
中用值填充列表,但是忘记将添加的新值通知适配器。
public void displayCities(List<CityResponse.City> cities) {
for (CityResponse.City city : cities) {
spinnerItem.add(city.getNama());
}
// 2. notify adapter that you have inserted new cities to the list
}
private void setSpinner(){
// 1. set the adapter with spinnerItem which is empty here
ArrayAdapter<String> adapter = new ArrayAdapter<>(..., ..., spinnerItem);
mainBinding.spinnerCity.setAdapter(adapter);
...
}
如果adapter
是setSpinner()
的局部变量,则displayCities()
无权访问adapter
。将adapter
创建为全局变量,然后可以在第2点注释中调用adapter.notiftDataSetChanged()
答案 1 :(得分:0)
您的setSpinner()方法:
private void setSpinner(final Context context, Spinner spinnerCity, List<Object> spinnerItem) {
final ArrayAdapter adapter = new ArrayAdapter(context, android.R.layout.simple_spinner_item, spinnerItem);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerCity.setAdapter(adapter);
spinnerCity.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String citySelected = parent.getItemAtPosition(position).toString();
//adapter.notifyDataSetChanged();
Toast.makeText(context, "City : " + citySelected, Toast.LENGTH_LONG).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
Toast.makeText(context, "Not Selected", Toast.LENGTH_LONG).show();
}
});
}
CityActivity.java:
Spinner spinnerCity = findViewById(R.id.spinner);
String[] spinnerItem = {"A", "B", "C", "D", "E"};
setSpinner(CityActivity.this, spinnerCity, Arrays.<Object>asList(spinnerItem));