如何在选择下拉列表项后获取下拉列表的值

huangapple go评论96阅读模式
英文:

How to get spinner value after item select in spinner

问题

I am using spinner in my app I am populating spinner with the data fetching from the server.I am using Retrofit2 as a networking library. There are 2 values I am fetching from the server one is state name and other is state id.

In spinner I am showing state name, but on select state it should select corresponding state name id that I have fetched from the server. In spinner state name is showing successfully but I want to gets corresponding state name id that I have in POJO class and not item position.

Below is my code:

Server response is given below:

  1. {
  2. "data": [
  3. {
  4. "id": "5",
  5. "name": "Bihar"
  6. },
  7. {
  8. "id": "7",
  9. "name": "Chhattisgarh"
  10. },
  11. {
  12. "id": "10",
  13. "name": "Delhi"
  14. }
  15. ],
  16. "status": true,
  17. "code": 200
  18. }

States.java

  1. public class States {
  2. @SerializedName("data")
  3. @Expose
  4. private List<AllStates> data = null;
  5. @SerializedName("status")
  6. @Expose
  7. private Boolean status;
  8. @SerializedName("code")
  9. @Expose
  10. private int code;
  11. public States() {
  12. }
  13. public List<AllStates> getData() {
  14. return data;
  15. }
  16. public void setData(List<AllStates> data) {
  17. this.data = data;
  18. }
  19. public Boolean getStatus() {
  20. return status;
  21. }
  22. public void setStatus(Boolean status) {
  23. this.status = status;
  24. }
  25. public int getCode() {
  26. return code;
  27. }
  28. public void setCode(int code) {
  29. this.code = code;
  30. }
  31. }

AllStates.java

  1. public class AllStates {
  2. @SerializedName("id")
  3. @Expose
  4. private String id;
  5. @SerializedName("name")
  6. @Expose
  7. private String name;
  8. public AllStates() {
  9. }
  10. public String getId() {
  11. return id;
  12. }
  13. public void setId(String id) {
  14. this.id = id;
  15. }
  16. public String getName() {
  17. return name;
  18. }
  19. public void setName(String name) {
  20. this.name = name;
  21. }
  22. }

CalenderFragment.java

  1. public class CalendarFragment extends Fragment {
  2. Spinner spinnerState;
  3. List<AllStates> stateList = new ArrayList<>();
  4. ArrayList<String> stateSpinnerList = new ArrayList<>();
  5. public CalendarFragment() {
  6. // Required empty public constructor
  7. }
  8. @Override
  9. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  10. Bundle savedInstanceState) {
  11. // Inflate the layout for this fragment
  12. View view = inflater.inflate(R.layout.fragment_calendar, container, false);
  13. spinnerState = view.findViewById(R.id.spinnerState);
  14. spinnerState.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
  15. @Override
  16. public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
  17. // Get the selected state object
  18. AllStates selectedState = stateList.get(position);
  19. // Now you can access the state's ID and Name
  20. String stateId = selectedState.getId();
  21. String stateName = selectedState.getName();
  22. Toast.makeText(parent.getContext(), "Selected: " + stateName + " (ID: " + stateId + ")", Toast.LENGTH_LONG).show();
  23. }
  24. @Override
  25. public void onNothingSelected(AdapterView<?> parent) {
  26. }
  27. });
  28. Retrofit retrofit = RetrofitClient.getInstance();
  29. ApiService apiService = retrofit.create(ApiService.class);
  30. apiService.allStates().subscribeOn(Schedulers.io())
  31. .observeOn(AndroidSchedulers.mainThread())
  32. .subscribe(new Observer<States>() {
  33. @Override
  34. public void onSubscribe(Disposable d) {
  35. }
  36. @Override
  37. public void onNext(States states) {
  38. stateList = states.getData();
  39. stateSpinnerList.add("Select state");
  40. for (int i = 0; i < stateList.size(); i++) {
  41. stateSpinnerList.add(stateList.get(i).getName());
  42. }
  43. ArrayAdapter<String> stateAdapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_list_item_1, stateSpinnerList);
  44. spinnerState.setAdapter(stateAdapter);
  45. }
  46. @Override
  47. public void onError(Throwable e) {
  48. TastyToast.makeText(getActivity(), e.getMessage(), TastyToast.LENGTH_SHORT,
  49. TastyToast.ERROR).show();
  50. }
  51. @Override
  52. public void onComplete() {
  53. }
  54. });
  55. return view;
  56. }
  57. }

How can I get the desired result?

英文:

I am using spinner in my app I am populating spinner with the data fetching from the server.I am using Retrofit2 as a networking library. There are 2 values I am fetching from the server one is state name and other is state id.

In spinner I am showing state name, but on select state it should select corresponding state name id that I have fetched from the server. In spinner state name is showing successfully but I want to gets corresponding state name id that I have in POJO class and not item position.

Below is my code:

Server response is given below:

  1. {
  2. &quot;data&quot;: [
  3. {
  4. &quot;id&quot;: &quot;5&quot;,
  5. &quot;name&quot;: &quot;Bihar&quot;
  6. },
  7. {
  8. &quot;id&quot;: &quot;7&quot;,
  9. &quot;name&quot;: &quot;Chhattisgarh&quot;
  10. },
  11. {
  12. &quot;id&quot;: &quot;10&quot;,
  13. &quot;name&quot;: &quot;Delhi&quot;
  14. }
  15. ],
  16. &quot;status&quot;: true,
  17. &quot;code&quot;: 200
  18. }

States.java

  1. public class States {
  2. @SerializedName(&quot;data&quot;)
  3. @Expose
  4. private List&lt;AllStates&gt; data = null;
  5. @SerializedName(&quot;status&quot;)
  6. @Expose
  7. private Boolean status;
  8. @SerializedName(&quot;code&quot;)
  9. @Expose
  10. private int code;
  11. public States(){
  12. }
  13. public List&lt;AllStates&gt; getData() {
  14. return data;
  15. }
  16. public void setData(List&lt;AllStates&gt; data) {
  17. this.data = data;
  18. }
  19. public Boolean getStatus() {
  20. return status;
  21. }
  22. public void setStatus(Boolean status) {
  23. this.status = status;
  24. }
  25. public int getCode() {
  26. return code;
  27. }
  28. public void setCode(int code) {
  29. this.code = code;
  30. }
  31. }

AllStates.java

  1. public class AllStates {
  2. @SerializedName(&quot;id&quot;)
  3. @Expose
  4. private String id;
  5. @SerializedName(&quot;name&quot;)
  6. @Expose
  7. private String name;
  8. public AllStates(){
  9. }
  10. public String getId() {
  11. return id;
  12. }
  13. public void setId(String id) {
  14. this.id = id;
  15. }
  16. public String getName() {
  17. return name;
  18. }
  19. public void setName(String name) {
  20. this.name = name;
  21. }
  22. }

CalenderFragment.java

  1. public class CalendarFragment extends Fragment {
  2. Spinner spinnerState;
  3. List&lt;AllStates&gt; stateList = new ArrayList&lt;&gt;();
  4. ArrayList&lt;String&gt; stateSpinnerList = new ArrayList&lt;&gt;();
  5. public CalendarFragment() {
  6. // Required empty public constructor
  7. }
  8. @Override
  9. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  10. Bundle savedInstanceState) {
  11. // Inflate the layout for this fragment
  12. View view = inflater.inflate(R.layout.fragment_calendar, container, false);
  13. spinnerState = view.findViewById(R.id.spinnerState);
  14. spinnerState.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
  15. @Override
  16. public void onItemSelected(AdapterView&lt;?&gt; parent, View view, int position, long id) {
  17. String item = parent.getItemAtPosition(position).toString();
  18. Toast.makeText(parent.getContext(), &quot;Selected: &quot; + item, Toast.LENGTH_LONG).show();
  19. }
  20. @Override
  21. public void onNothingSelected(AdapterView&lt;?&gt; parent) {
  22. }
  23. });
  24. Retrofit retrofit = RetrofitClient.getInstance();
  25. ApiService apiService = retrofit.create(ApiService.class);
  26. apiService.allStates().subscribeOn(Schedulers.io())
  27. .observeOn(AndroidSchedulers.mainThread())
  28. .subscribe(new Observer&lt;States&gt;() {
  29. @Override
  30. public void onSubscribe(Disposable d) {
  31. }
  32. @Override
  33. public void onNext(States states) {
  34. stateList = states.getData();
  35. stateSpinnerList.add(&quot;Select state&quot;);
  36. for(int i =0;i&lt;stateList.size();i++){
  37. stateSpinnerList.add(stateList.get(i).getName());
  38. }
  39. ArrayAdapter&lt;String&gt; stateAdapter = new ArrayAdapter&lt;&gt;(getActivity(),android.R.layout.simple_list_item_1,stateSpinnerList);
  40. spinnerState.setAdapter(stateAdapter);
  41. }
  42. @Override
  43. public void onError(Throwable e) {
  44. TastyToast.makeText(getActivity(),e.getMessage(),TastyToast.LENGTH_SHORT,
  45. TastyToast.ERROR).show();
  46. }
  47. @Override
  48. public void onComplete() {
  49. }
  50. });
  51. return view;
  52. }
  53. }

How can I get the desired result?

答案1

得分: 3

所以我理解的是,您正在从 Spinner 中选择州,并获取州的名称,但您想要获取 stateId。

我能想到的一个可能的解决方案是使用 HashMap<StateName, StateId> 映射。
当您从 Spinner 接收州的名称时,只需调用 map.get(name),您就会获得 state Id。

编辑:

您可以像这样实现它:

  1. HashMap&lt;String, Integer&gt; map = new HashMap&lt;&gt;();
  2. for(state in list){
  3. map.put(state.name, state.id);
  4. }
  5. spinnerState.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
  6. @Override
  7. public void onItemSelected(AdapterView&lt;?&gt; parent, View view, int position, long id) {
  8. String name = parent.getItemAtPosition(position).toString();
  9. int id = map.get(name);
  10. }
  11. @Override
  12. public void onNothingSelected(AdapterView&lt;?&gt; parent) {
  13. }
  14. });
英文:

So what I understand is you are selecting state from Spinner and getting name of state but you want to get stateId.

one possible solution, I can think of is to use HashMap<StateName, StateId> map.
when you receive state name from spinner just call map.get(name) and you will get state Id.

Edit:

you can implement it like this

  1. HashMap&lt;String, Integer&gt; map = new HashMap&lt;&gt;();
  2. for(state in list){
  3. map.put(state.name, state.id);
  4. }
  5. spinnerState.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
  6. @Override
  7. public void onItemSelected(AdapterView&lt;?&gt; parent, View view, int position, long id) {
  8. String name = parent.getItemAtPosition(position).toString();
  9. int id = map.get(name);
  10. }
  11. @Override
  12. public void onNothingSelected(AdapterView&lt;?&gt; parent) {
  13. }
  14. });

答案2

得分: 1

如果您不想使用自定义适配器,那么可以获取所选微调器位置,并使用该位置获取您的 "State" 模型。

  1. spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
  2. @Override
  3. public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
  4. // int position = spinnerState.getSelectedItemPosition();
  5. if (position >= 1) {
  6. AllStates allStates = stateList.get(position - 1);
  7. }
  8. }
  9. @Override
  10. public void onNothingSelected(AdapterView<?> parent) {
  11. }
  12. });

现在您可以从 "allStates" 模型中访问 "id"。

英文:

If you don't want to use a custom adapter then fetch selected spinner position and using that position fetch your State model

  1. spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
  2. @Override
  3. public void onItemSelected(AdapterView&lt;?&gt; parent, View view, int position, long id) {
  4. // int position = spinnerState.getSelectedItemPosition();
  5. if(position &gt;= 1){
  6. AllStates allStates = stateList.get(position - 1)
  7. }
  8. }
  9. @Override
  10. public void onNothingSelected(AdapterView&lt;?&gt; parent) {
  11. }
  12. });

Now you can access id from allStates model

huangapple
  • 本文由 发表于 2020年7月25日 17:47:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/63086853.html
匿名

发表评论

匿名网友

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

确定