-4

I am getting users data from server and load it in spinner till here it works fine, following is my json response

{

"user_city": "Kolkata",
"user_state": "West Bengal",

}

now it will set to my spinner,now if user want to change state then i have another service for states,there i have all the states,but how to get all states when user click on spinner..

class LoadAllProdetails extends
            AsyncTask<String, String, ArrayList<HashMap<String, String>>> {
        private ProgressDialog pDialog;
        private String test;
        private JSONObject jsonObjsss;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(Profiles.this.getActivity());
            pDialog.setMessage("Please wait..");
            pDialog.setIndeterminate(true);
            pDialog.setIndeterminateDrawable(getResources().getDrawable(R.drawable.custom_progress));
            pDialog.setCancelable(true);
            pDialog.show();
        }
        protected ArrayList<HashMap<String, String>> doInBackground(
                String... args) {
            ServiceHandler sh = new ServiceHandler();
            // Making a request to url and getting response

            String jsonStr = sh.makeServiceCall(GET_PRO_DETAILS, ServiceHandler.GET);
            Log.d("Response: ", "> " + jsonStr);
            if (jsonStr != null) {
                try {
                    jsonObjsss = new JSONObject(jsonStr);
                    // state_list = jsonObj.getJSONArray(COUNTRY_LIST);
                    // looping through All Contacts

                    profilessstates=new ArrayList<HashMap<String,String>>();

                    profilecitis=new ArrayList<HashMap<String,String>>();

                    if(jsonObjsss.getString(GET_PRO_USERSTATUS).equals("0"))
                    {
                        final String msgs=jsonObjsss.getString("message");
                        System.out.println("Messagessss"+msgs);
                        getActivity().runOnUiThread(new  Runnable()
                        {
                            @Override
                            public void run()
                            {

                                Toast.makeText(getActivity(), msgs, Toast.LENGTH_LONG).show();
                            }
                        });
                    }
                    else if(jsonObjsss.getString(GET_PRO_USERSTATUS).equals("1")) {

                        HashMap<String, String> mapzz = new HashMap<String, String>();
                        usersstatus = jsonObjsss.getString(GET_PRO_USERSTATUS);
                        usersfname = jsonObjsss.getString(GET_PRO_FIRSTNAME);
                        usersmails = jsonObjsss.getString(GET_PRO_EMAILS);
                        usersmob = jsonObjsss.getString(GET_PRO_MOBILE);
                        usersdobs = jsonObjsss.getString(GET_PRO_DATES);
                        usersaddresss = jsonObjsss.getString(GET_PRO_ADDRESS);
                        userszipss = jsonObjsss.getString(GET_PRO_ZIP);
                        userstates=jsonObjsss.getString(GET_PRO_STATE);
                        usercitys=jsonObjsss.getString(GET_PRO_CITY);

                        mapzz.put(GET_PRO_STATE, jsonObjsss.getString(GET_PRO_STATE));

                        mapzz.put(GET_PRO_CITY, jsonObjsss.getString(GET_PRO_CITY));
                        profilessstates.add(mapzz);

                        profilecitis.add(mapzz);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            } else {
                Log.e("ServiceHandler", "Couldn't get any data from the url");
            }
            return profilessstates;
        }
        protected void onPostExecute(ArrayList<HashMap<String, String>> result) {
            super.onPostExecute(result);
            pDialog.dismiss();
            updtfname.setText(usersfname);
            updtmail.setText(usersmails);
            updtmob.setText(usersmob);
            updtaddress.setText(usersaddresss);
            updtpin.setText(userszipss);
            datestext.setText(usersdobs);

            arrprostates = new String[profilessstates.size()];
            for (int index = 0; index < profilessstates.size(); index++) {
                HashMap<String, String> map = profilessstates.get(index);
                arrprostates[index] = map.get(GET_PRO_STATE);
            }
            adapterprostates = new ArrayAdapter<String>(
                    Profiles.this.getActivity(),
                    android.R.layout.simple_spinner_dropdown_item, arrprostates);
            statespinner.setAdapter(adapterprostates);



            arrprocities = new String[profilecitis.size()];
            for (int index = 0; index < profilecitis.size(); index++) {
                HashMap<String, String> map = profilecitis.get(index);
                arrprocities[index] = map.get(GET_PRO_CITY);
            }
            adapterprocities = new ArrayAdapter<String>(
                    Profiles.this.getActivity(),
                    android.R.layout.simple_spinner_dropdown_item, arrprocities);
            cityspinner.setAdapter(adapterprocities);

To load all states

class LoadStatess extends
            AsyncTask<String, String, ArrayList<HashMap<String, String>>> {
        private ProgressDialog pDialog;
        private String test;
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(Profiles.this.getActivity());
            pDialog.setMessage("Please wait..");
            pDialog.setIndeterminate(true);
            pDialog.setIndeterminateDrawable(getResources().getDrawable(R.drawable.custom_progress));
            pDialog.setCancelable(true);
            pDialog.show();
        }
        protected ArrayList<HashMap<String, String>> doInBackground(
                String... args) {
            ServiceHandler sh = new ServiceHandler();
            // Making a request to url and getting response
            statedata = new ArrayList<HashMap<String, String>>();
            String jsonStr = sh.makeServiceCall(STATE_URL, ServiceHandler.GET);
            Log.d("Response: ", "> " + jsonStr);
            if (jsonStr != null) {
                try {
                    jsonObj = new JSONArray(jsonStr);
                    // state_list = jsonObj.getJSONArray(COUNTRY_LIST);
                    // looping through All Contacts
                    for (int i = 0; i < jsonObj.length(); i++) {
                        JSONObject c = jsonObj.getJSONObject(i);
                        // creating new HashMap
                        HashMap<String, String> map = new HashMap<String, String>();
                        // adding each child node to HashMap key => value
                        map.put(USER_STATUSS, c.getString(USER_STATUSS));
                        map.put(PRESET_TITLES, c.getString(PRESET_TITLES));
                        statedata.add(map);

                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            } else {
                Log.e("ServiceHandler", "Couldn't get any data from the url");
            }
            return statedata;
        }
        protected void onPostExecute(ArrayList<HashMap<String, String>> result) {
            super.onPostExecute(result);
            pDialog.dismiss();
            arrallstates = new String[statedata.size()];
            for (int index = 0; index < statedata.size(); index++) {
                HashMap<String, String> map = statedata.get(index);
                arrallstates[index] = map.get(PRESET_TITLES);
            }
            // pass arrConuntry array to ArrayAdapter<String> constroctor :
            adapterallstates = new ArrayAdapter<String>(
                    Profiles.this.getActivity(),
                    android.R.layout.simple_spinner_dropdown_item, arrallstates);
            statespinner.setAdapter(adapterallstates);
            statespinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                    spitems = statespinner.getSelectedItem().toString();
                    System.out.println("PresetEVent selected" + spitems);





                    new Logincity().execute();
                }

                @Override
                public void onNothingSelected(AdapterView<?> adapterView) {
                }
            });
        }
    }
Nimantha
  • 6,405
  • 6
  • 28
  • 69
albert
  • 251
  • 5
  • 20

1 Answers1

0

XML file:

<Spinner android:id="@+id/Spinner01"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

Java file:

public class SpinnerExample extends Activity {

    private String[] arraySpinner;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        //json to list
        ArrayList<String> list = new ArrayList<String>();     
        JSONArray jsonArray = (JSONArray)jsonObject; 
        if (jsonArray != null) { 
               int len = jsonArray.length();
               for (int i=0;i<len;i++){ 
                     list.add(jsonArray.get(i).toString());
               } 
        }
        //add list to spinner
        Spinner s = (Spinner) findViewById(R.id.Spinner01);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, list);
        s.setAdapter(adapter);
    }
}
Nimantha
  • 6,405
  • 6
  • 28
  • 69
Dinithe Pieris
  • 1,822
  • 3
  • 32
  • 44