Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
713 views
in Technique[技术] by (71.8m points)

dynamic - Set value for Spinner with custom Adapter in Android

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...