我如何添加json响应下拉和当我选择国家显示状态

时间:2015-12-30 07:36:26

标签: android android-activity

  • 我的json对国家的回复是:{"search values":[{"id":"1","name":"India"},{"id":"2","name":"srilanka"}],"success":1}
  • 国家/地区添加到精简版。
  • 我的要求当我选择国家/地区发布请求国家/地区“id”(动态)和表名称(静态)时。
  • 然后我得到状态列表,这是({"searchvalues":[{"id":"1","name":"Tamilnadu"},{"id":"1","name":"Kerala"}],"success":1}).

    我的代码是:

     protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.registration);
    
    
        //spinners
        country=(Spinner)findViewById(R.id.country);
        state=(Spinner)findViewById(R.id.state);
        dist=(Spinner)findViewById(R.id.dist);
        mandal=(Spinner)findViewById(R.id.mandal);
    
        //edit text
        uname=(EditText)findViewById(R.id.name);
        mobileno=(EditText)findViewById(R.id.mobileno);
        companyname=(EditText)findViewById(R.id.cname);
        village=(EditText)findViewById(R.id.village);
        landmark=(EditText)findViewById(R.id.landmark);
        pincode=(EditText)findViewById(R.id.pincode);
        pwd=(EditText)findViewById(R.id.pwd);
        cpwd=(EditText)findViewById(R.id.cpwd);
        dstatus=(EditText)findViewById(R.id.delivery);
    
        //Button
        submit=(Button)findViewById(R.id.submit);
    
        //adapter for drop down
        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item);
    
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    
        queue=Volley.newRequestQueue(this);
    
        submit.setOnClickListener(new OnClickListener() {
    
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
    
                StringRequest sr = new StringRequest(Request.Method.POST, REG_URL, new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
    
                    }
                }, new Response.ErrorListener() {
    
                    @Override
                    public void onErrorResponse(VolleyError e) {
                        // TODO Auto-generated method stub
                        e.printStackTrace();
                    }
                }){
    
                };
                queue.add(sr);
    
            }
    
        });
    
        //Getting Country spinnervalues
        StringRequest stringRequest = new StringRequest(com.android.volley.Request.Method.POST, D_URL,
                new Response.Listener<String>() {
    
                    @Override
                    public void onResponse(String response) {
                        Log.d("response","response => "+response);
                        try {
                            JSONObject jsnobject = new JSONObject(response);
                        JSONArray  jsonArray = jsnobject.getJSONArray("searchvalues");
                        Log.d("jsonArray","jsonArray => "+jsonArray);
                         for (int i = 0; i < jsonArray.length(); i++) {
                             adapter.add(jsonArray.getJSONObject(i).getString("name"));
                         }
    
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
    
                    }
    
    },  new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
             Log.d("ERROR","error => "+error.toString());
        }
    }){
             @Override
                protected Map<String, String> getParams() 
                {  
                 Map<String, String> params = new HashMap<String, String>();
                    params.put("table","country");
                    System.out.println("params-------------->"+ params);
                    return params;
                }
    
        };
        queue.add(stringRequest);
    
        country.setAdapter(adapter);
      country.setOnItemSelectedListener(new OnItemSelectedListener() {
    
        @Override
        public void onItemSelected(AdapterView<?> parent, View view,
                int position, long id) {
    
            StringRequest sr = new StringRequest(Request.Method.POST, REG_URL, new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    // TODO Auto-generated method stub
    
                }
            }, new Response.ErrorListener() {
    
                @Override
                public void onErrorResponse(VolleyError e) {
                    // TODO Auto-generated method stub
                    e.printStackTrace();
                }
            }){
    
                @Override
                protected Map<String, String> getParams() 
                {  
                 Map<String, String> params = new HashMap<String, String>();
                    params.put("table","state");
                 //   params.put("id",position);
                    System.out.println("params-------------->"+ params);
                    return params;
                }
    
            };
            queue.add(sr);
    
        }
    
        @Override
        public void onNothingSelected(AdapterView<?> parent) {
    
    
        }
    });
    }
    }
    

1 个答案:

答案 0 :(得分:0)

When you are selecting a country on its item select method write volley request    

country.setOnItemSelectedListener(new OnItemSelectedListener() {

@Override
public void onItemSelected(AdapterView<?> parent, View view,
        int position, long id) {

    StringRequest sr = new StringRequest(Request.Method.POST, REG_URL, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            // TODO Auto-generated method stub

          /**get your states from response and save it in array and pass this array to arrayadapter**/
        }
         //Now setadapter with spinner
    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError e) {
            // TODO Auto-generated method stub
            e.printStackTrace();
        }
    })



    };
    queue.add(sr);

}

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


}
});