Set value for Spinner with custom Adapter in Android

ぃ、小莉子 提交于 2019-12-13 09:59:46

问题


I am developing a android application with spinner in a form. The spinner items and spinner values are different. I want to collect all the value from the from including spinner and set a rest api service to web back end.

here is the response array.

{"Status":true,"errorType":null,"countryList":[{"Code":"US","Name":"United States"},{"Code":"CA","Name":"Canada"},{"Code":"AU","Name":"Australia"},{"Code":"GB","Name":"United Kingdom"}]}

I am successfully binded the Name jsonObject to spinner but I cant add the Code.

here is my code.

JSONObject responseObject = new JSONObject(res);
            String status=responseObject.getString("Status");
            JSONArray JA = responseObject.getJSONArray("countryList");
            JSONObject json= null;


            if(status.equals("true")) {

                final String[] str2 = new String[JA.length()];
                for (int i=0;i<JA.length();i++)
                {
                    json = JA.getJSONObject(i);
                    str2[i] = json.getString("Name");
                }

                sp = (Spinner) findViewById(R.id.citnzshp_field);
                list = new ArrayList<String>();

                for(int i=0;i<str2.length;i++)
                {
                    list.add(str2[i]);

                }
                Collections.sort(list);

                ArrayAdapter<String> adp;
                adp = new ArrayAdapter<String>
                        (getApplicationContext(),android.R.layout.simple_dropdown_item_1line, list);

                sp.setAdapter(adp);


            }

How can I bind the code jsonObject to spinner for taking after form submits.

Can anyone please share me the advantage of making custom adapter for binding data to spinner and selecting value also.


回答1:


@Haresh Chhelana example is good, However if you want to show both name and code in spinner after selecting, check this out.

    List<Map<String, String>> items = new ArrayList<Map<String, String>>();

    for (int i = 0; i < JA.length(); i++) {
        json = JA.getJSONObject(i);
        mapData = new HashMap<String, String>();
        mapData.put("name", json.getString("Name"));
        mapData.put("code", json.getString("Code"));
        items.add(mapData);
    }

    SimpleAdapter adapter = new SimpleAdapter(this, items, android.R.layout.simple_list_item_2, new String[] {
            "name", "code" }, new int[] { android.R.id.text1, android.R.id.text2 });
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter);

And Spinner selected item callback

spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                Map<String, String> selectedItem = (Map<String, String>) parent.getSelectedItem();
                String name=selectedItem.get("name");
                String code=selectedItem.get("code");
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });


来源:https://stackoverflow.com/questions/30973536/set-value-for-spinner-with-custom-adapter-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!