Set value for Spinner with custom Adapter in Android

5.4k views Asked by At

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

There are 1 answers

5
Bharatesh On BEST ANSWER

@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) {
            }
        });