如何在Spinner中加载替换json值? [英] How to load replace json values in spinner?

查看:53
本文介绍了如何在Spinner中加载替换json值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是android的新手,我有一个应用程序,在我的应用程序中,我正在获取用户的个人资料数据,在这里,在我的个人资料数据中,我正在获取用户先前设置的用户所在的州和城市,就像现在用户设置了以下状态并城市,而且我可以在微调器中显示该州和城市

I am new in android,I have one application,in my application i am getting user's profile data,here in my profile data i am getting user's state and city which user had previously set,like right now user set following state and city,and i am able to display that state and city in my spinner

{

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

}

现在的问题是,如果用户想要将状态从WestBengal更改为卡纳塔克邦,那么我需要显示所有状态并使用户更改,因此为此我有其他单独的Web服务来加载所有状态,并且我想显示所有状态状态与微调器处于同一状态,但现在的问题是我需要单击两次,然后才显示所有状态

Now issue is if user want to change state like from WestBengal to Karnataka,then i need to display all state and make user to change,so for that i have other separate webservice for load all state and i want to display that all state in same that spinner,but right now issue is i need to click two times then only it is showing all states

这是回应

[{"user_status":"1","state_id":"1","state":"Karnataka"},{"user_status":"1","state_id":"2","state":"Tamilnadu"},{"user_status":"1","state_id":"3","state":"Maharastra"},{"user_status":"1","state_id":"4","state":"Andhra Pradesh"},{"user_status":"1","state_id":"5","state":"West Bengal"},{"user_status":"1","state_id":"6","state":"Delhi"},{"user_status":"1","state_id":"8","state":"Andaman & Nicobar Islands"},{"user_status":"1","state_id":"9","state":"Arunachal Pradesh"},{"user_status":"1","state_id":"10","state":"Bihar"},{"user_status":"1","state_id":"11","state":"Chattisgarh"},{"user_status":"1","state_id":"12","state":"Dadra & Nagar Haveli"},{"user_status":"1","state_id":"13","state":"Daman & Diu"},{"user_status":"1","state_id":"14","state":"Goa"},{"user_status":"1","state_id":"15","state":"Gujarat"},{"user_status":"1","state_id":"16","state":"Haryana"},{"user_status":"1","state_id":"17","state":"Himachal Pradesh"},{"user_status":"1","state_id":"18","state":"Jharkhand"},{"user_status":"1","state_id":"19","state":"Kerala"},{"user_status":"1","state_id":"20","state":"Lakshadweep"},{"user_status":"1","state_id":"21","state":"Madhya pradesh"},{"user_status":"1","state_id":"22","state":"Pondichery"},{"user_status":"1","state_id":"23","state":"Punjab"},{"user_status":"1","state_id":"24","state":"Rajasthan"},{"user_status":"1","state_id":"25","state":"Sikkim"},{"user_status":"1","state_id":"26","state":"Telangana"},{"user_status":"1","state_id":"27","state":"Tripura"},{"user_status":"1","state_id":"28","state":"Uttaranchal"},{"user_status":"1","state_id":"29","state":"Uttar Pradesh"},{"user_status":"1","state_id":"31","state":"Nagaland"},{"user_status":"1","state_id":"32","state":"Mizoram"},{"user_status":"1","state_id":"33","state":"Meghalaya"},{"user_status":"1","state_id":"34","state":"Manipur"},{"user_status":"1","state_id":"35","state":"Assam"},{"user_status":"1","state_id":"36","state":"Chandigarh"},{"user_status":"1","state_id":"37","state":"Orissa"},{"user_status":"1","state_id":"38","state":"Others"}]

加载配置文件

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

加载所有状态

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.setPrompt("Select State");
            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) {
                }
            });
        }
    }

默认情况下,第一个屏幕显示

first screen by default get

然后,我单击加载状态它显示

then i click to load state it shows

然后我再次点击显示的唯一内容

then again i click the only it shows

推荐答案

另一个是要

    @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            LoadListView();
    }

    private void LoadListView() {
            try {
               statespinner.setAdapter(adapterprostates);

            } catch (Exception e) {
               e.printStacktrace();
            }
        }

这篇关于如何在Spinner中加载替换json值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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