【问题标题】:How can I load json object to Spinner?如何将 json 对象加载到 Spinner?
【发布时间】:2015-12-22 08:03:46
【问题描述】:

我正在从服务器获取用户数据并将其加载到微调器中,直到这里它工作正常,以下是我的 json 响应

{

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

}

现在它将设置为我的微调器,现在如果用户想要更改状态,那么我有另一个状态服务,我有所有状态,但是当用户单击微调器时如何获取所有状态..

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

【问题讨论】:

  • 你尝试了什么?
  • @ρяσѕρєяK 查看我编辑的问题..我能够显示“user_city”:“加尔各答”,“user_state”:“西孟加拉邦”,
  • @ρяσѕρєяK 现在我无法显示所有状态..
  • @UmaKanth 查看我编辑的问题

标签: android json android-spinner


【解决方案1】:

XML 文件:

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

Java 文件:

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

【讨论】:

  • 将所有状态放入数组或列表中,然后将其添加到适配器
猜你喜欢
  • 1970-01-01
  • 2021-10-24
  • 2018-08-16
  • 1970-01-01
  • 1970-01-01
  • 2017-05-24
  • 2012-06-15
  • 2016-11-21
  • 1970-01-01
相关资源
最近更新 更多