对于微调设定值与Android的定制适配器 [英] Set value for Spinner with custom Adapter in Android

查看:199
本文介绍了对于微调设定值与Android的定制适配器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发的形式与微调一个Android应用程序。 的纺纱项目和微调值是不同的。我想从包括微调收集来自所有价值,并设置一个REST API的服务网络后端。

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.

下面是响应阵列。

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

我成功地绑定在名称的JSONObject来微调器,但我不能添加在 code

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

这是我的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);


            }

我如何可以将绑定在 code 的JSONObject来微调器表单提交后服用。

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.

推荐答案

@Haresh Chhelana例子是好的,但是如果你想要选择后显示微调两种名称和code,检查了这一点。

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

和微调所选项目的回调

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

这篇关于对于微调设定值与Android的定制适配器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆