如何将jSON响应加载到spinner中,相应于使用volley

时间:2016-11-28 06:30:31

标签: android json android-volley

我的android代码中有一个城市微调器,我要做的是当我从城市微调器中选择城市时,相应城市微调器的ID应该转到服务器并根据区域将显示在区域微调器中区域微调器应该依赖于JSON响应。对于城市微调器,我已经给出了硬编码字符串。

例如每当我从城市微调器中选择城市nashik时,根据大学路的区域,加拿大角应该在区域微调器中显示。

JSON响应:

SEARCHED_AREA[{"area_name":"Panchavati"},{"area_name":"Ashok Stambh"}]

我试过的代码: Spinner spinnerCustom =(Spinner)findViewById(R.id.spinnerCustom);

    ArrayList<String> cities = new ArrayList<String>();
    cities.add("Amravati");
    cities.add("Aurangabad");
    cities.add("Kalyan Dombivali");
    cities.add("Mumbai");
    cities.add("Nashik");
    cities.add("Navi Mumbai");
    cities.add("Pune-Pimpri Chinchwad");
    cities.add("Solapur");
    cities.add("Thane");
    CustomSpinnerAdapter customSpinnerAdapter=new CustomSpinnerAdapter(HomeActivity.this,cities);
    spinnerCustom.setAdapter(customSpinnerAdapter);
    spinnerCustom.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
             city_id=position+1;
            Toast.makeText(parent.getContext(),"id"  +String.valueOf(city_id) , Toast.LENGTH_LONG).show();
            registercityid();
        }

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

        }
    });
}public void registercityid() {
    StringRequest stringRequest = new StringRequest(Request.Method.POST, REGISTER_URL,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    Toast.makeText(HomeActivity.this,response,Toast.LENGTH_LONG).show();
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(HomeActivity.this,error.toString(),Toast.LENGTH_LONG).show();
                }
            }){
        @Override
        protected Map<String,String> getParams(){
            Map<String,String> params = new HashMap<String, String>();
            params.put(KEY_ID,String.valueOf(city_id));

            return params;

        }

    };
    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(stringRequest);

}

有人可以提供示例代码吗? 提前谢谢。

2 个答案:

答案 0 :(得分:2)

您可以通过以下方式实现此目的......按照说明和步骤操作。

我创建了一个从州到城市过滤微调器数据的示例。你可以按照你的要求去做。

1)首先在oncreate() mehtod。

中的微调器适配器中设置状态数据

2)创建一个具有城市数据的方法,并设置城市的微调器适配器。此方法实际上刷新了微调器,因为状态微调器选择的值已更改。 (在你的情况下,你可以在这里解析你的json数据并设置为适配器。)

private void setCity() {

    // String array that having city data.
    String City_gj[] = {"Ahmedabad", "Vadodara", "Surat", "Rajkot"};
    String City_mh[] = {"Mumbai", "Pune", "Lonavala"};
    String City_rj[] = {"Udaipur", "jaipur", "Bikaner", "jaisalmer"};

    // here sp_state is my state spinner. I' getting current selected state value and according to it i am setting my city spinner adapter.
    if (sp_state.getSelectedItem().equals("Gujarat")) {
        ArrayAdapter Adpt_City = new ArrayAdapter(this, R.layout.spinner_textview, City_gj);
        //sp_city is my city spinner.
        sp_city.setAdapter(Adpt_City);
    }

    if (sp_state.getSelectedItem().equals("Maharastra")) {
        ArrayAdapter Adpt_City = new ArrayAdapter(this, R.layout.spinner_textview, City_mh);
        sp_city.setAdapter(Adpt_City);
    }

    if (sp_state.getSelectedItem().equals("Rajasthan")) {
        ArrayAdapter Adpt_City = new ArrayAdapter(this, R.layout.spinner_textview, City_rj);
        sp_city.setAdapter(Adpt_City);
    }
}

3)最后,在这里进入onCreate() mehthod simple生成你的状态微调器列表器,如下所示,并在setCity()中设置这个onItemSelected方法。

sp_state.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {

            setCity();
        }

        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {

        }
    });

答案 1 :(得分:2)

下面是示例代码,试一试....

<!DOCTYPE html>
<html>
<head>
    <title>Table styling</title>
    <meta charset="utf-8" />
    <meta name="format-detection" content="telephone=no"/>
    <style type="text/css">
            table, th, td {
                font-family: "Trebuchet MS", Arial, sans-serif;
            }
            caption {
                font-size: 150%;
                font-weight: bold;
                margin: 5px;
            }
            table { 
                width: 750px; 
                border-collapse: collapse; 
                margin:50px auto;
                }
            /* Zebra striping */
            tr:nth-of-type(odd) { 
                background: #eee; 
                }
            th { 
                background: #3498db; 
                color: white; 
                font-weight: bold; 
                }
            td, th { 
                padding: 10px; 
                border: 1px solid #ccc; 
                text-align: left; 
                font-size: 18px;
                }
        tfoot td{
            background: green; 
            color: white; 
            font-weight: bold;
        }
            tr:hover {
                    color: yellow;
                    background: orangered;
                    text-decoration: underline !important;
                }
    </style>
<body>

    <table>
    <thead>
            <th>First Name</th>
            <th>Last Name</th>
            <th>Job Title</th>
            <th>Twitter</th>
    </thead>
    <tbody>
        <tr>
            <td data-column="First Name">James</td>
            <td data-column="Last Name">Matman</td>
            <td data-column="Job Title">Chief Sandwich Eater</td>
            <td data-column="Twitter">@james</td>
        </tr>
        <tr>
            <td data-column="First Name">Andor</td>
            <td data-column="Last Name">Nagy</td>
            <td data-column="Job Title">Designer</td>
            <td data-column="Twitter">@andornagy</td>
        </tr>
        <tr>
            <td data-column="First Name">Tamas</td>
            <td data-column="Last Name">Biro</td>
            <td data-column="Job Title">Game Tester</td>
            <td data-column="Twitter">@tamas</td>
        </tr>
        <tr>
            <td data-column="First Name">Zoli</td>
            <td data-column="Last Name">Mastah</td>
            <td data-column="Job Title">Developer</td>
            <td data-column="Twitter">@zoli</td>
        </tr>
        <tr>
            <td data-column="First Name">Szabi</td>
            <td data-column="Last Name">Nagy</td>
            <td data-column="Job Title">Chief Sandwich Eater</td>
            <td data-column="Twitter">@szabi</td>
        </tr>
    </tbody>
    <tfoot>
        <td colspan="3">Total</td>
        <td >4</td>
    </tfoot>
</table>


</body>
</html>

现在在arrayList中添加数据如下: -

public class CityDetailModel {
    String cityName, cityID;

    public CityDetailModel(String cityName, String cityID) {
        this.cityName = cityName;
        this.cityID = cityID;
    }

    public String getCityName() {
        return cityName;
    }

    public void setCityName(String cityName) {
        this.cityName = cityName;
    }

    public String getCityID() {
        return cityID;
    }

    public void setCityID(String cityID) {
        this.cityID = cityID;
    }
}

现在在onItemSelected()方法中,从列表中获取该城市的ID,如下所示: -

    ArrayList<CityDetailModel> cities = new ArrayList<CityDetailModel>();
    cities.add(new CityDetailModel("City1", "CityID 1")); // add appropriate city id according to your requirements
    cities.add(new CityDetailModel("City2", "CityID 2"));
    cities.add(new CityDetailModel("City3", "CityID 3"));
    cities.add(new CityDetailModel("City4", "CityID 4"));
    cities.add(new CityDetailModel("City5", "CityID 5"));

现在,在您的服务电话中传递此cityID,您将获得您的区域响应。希望它能帮到你......