如何在Android的Spinner中设置一个字符串值,而不使用ArrayList?

huangapple 未分类评论54阅读模式
英文:

How to set a String value in the spinner without ArrayList in Android?

问题

用户位置已保存在数据库中。在这里,我正在从数据库中检索州和城市,然后保存在字符串中。这部分没问题。现在,我该如何将州的字符串值设置在一个下拉列表中,然后将城市的字符串值设置在第二个下拉列表中?

String state = "prov";
String city = "cityy";

当我尝试以下操作时:

ArrayAdapter<String> adaptersta = new ArrayAdapter<String>(uploadpostj.this, android.R.layout.simple_spinner_item, Collections.singletonList(state)); 
spinnerstate.setAdapter(adaptersta);

以及

ArrayAdapter<String> adaptercityy = new ArrayAdapter<String>(uploadpostj.this, android.R.layout.simple_spinner_item, Collections.singletonList(city)); 
spinnercity.setAdapter(adaptercityy);

当我只尝试设置城市值时,一切正常,或者只尝试设置州值,同样没问题。然而,当我尝试同时设置两个值时,只有州的值被设置,而城市为空。我如何在两个下拉列表中都设置这两个字符串值?

如何在Android的Spinner中设置一个字符串值,而不使用ArrayList?

英文:

Users location is already saved in database. Here, Im retrieving state and city from database, then saved in strings. That is ok. Now, how can I set state string value in a spinner and then city string value in a 2nd spinner?

String state = &quot;prov&quot;; 
String city = &quot;cityy&quot;;

When I try

ArrayAdapter&lt;String&gt; adaptersta = new ArrayAdapter&lt;String&gt;(uploadpostj.this, android.R.layout.simple_spinner_item, Collections.singletonList(state)); 
spinnerstate.setAdapter(adaptersta);

and

ArrayAdapter&lt;String&gt; adaptercityy = new ArrayAdapter&lt;String&gt;(uploadpostj.this, android.R.layout.simple_spinner_item, Collections.singletonList(city)); 
spinnercity.setAdapter(adaptercityy);

Then if I just try to set only city value, then it's ok, or try to set only state value, again ok. However, when I try to set both values, then only state value is set and city is empty. How can I set both string values in both spinners?

如何在Android的Spinner中设置一个字符串值,而不使用ArrayList?

答案1

得分: 0

用户选择状态后,您应将该状态的城市添加到该状态的数组列表中,并在城市下拉列表中显示。

英文:

After state is selected by user, you should add cities of that state to arraylist of that state and show it in city spinner.

huangapple
  • 本文由 发表于 2020年7月25日 18:42:54
  • 转载请务必保留本文链接:https://java.coder-hub.com/63087354.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定