如何根据微调器选择获取json对象

时间:2015-11-23 06:58:25

标签: android arraylist android-spinner jsonobject

我从这个回复中得到了我的微调器的名称和日期,现在我正在尝试的是如果用户从spinner中选择lily然后我想在字符串中获取occ值,但我没有得到它,以下是我的回复和代码,任何人都可以告诉我这是什么问题

[
    {
        "friend_id": "1",
        "user_spoc_dob": "1993-11-11",
        "status": "true",
        "spouse_name": "lily",
        "occ": "spoc"
    },
    {
        "friend_id": "1",
        "user_mother_dob": "1974-12-12",
        "status": "true",
        "mother_name": "sita",
        "message": "Address details Not available",
        "occ": "mom"
    },
    {
        "friend_id": "1",
        "user_father_dob": "1972-11-19",
        "status": "true",
        "father_name": "ram",
        "message": "Address details Not available",
        "occ": "dad"
    },
    {
        "friend_id": "1",
        "user_dob": "1994-02-20",
        "status": "true",
        "user_fname": "Su",
        "occ": "spl"
    }
]

MainActivity.java

 protected ArrayList<String> doInBackground(
                String... args) {
            ServiceHandler sh = new ServiceHandler();

            statedata = new ArrayList<String>();
            String jsonStr = sh.makeServiceCall(STATE_URL, ServiceHandler.GET);
            Log.d("Response: ", "> " + jsonStr);
            if (jsonStr != null) {
                try {
                    jsonObj = new JSONArray(jsonStr);
                    for (int i = 0; i < jsonObj.length(); i++) {


                        JSONObject c = jsonObj.getJSONObject(i);
                        String wifebday= (c.has("user_spoc_dob")) ? c.getString("user_spoc_dob") : null;
                        String wifename= (c.has("spouse_name")) ? c.getString("spouse_name") : null;
                        String mothersbday= (c.has("user_mother_dob")) ? c.getString("user_mother_dob") : null;
                        String mothersname= (c.has("mother_name")) ? c.getString("mother_name") : null;
                        String fatherbday= (c.has("user_father_dob")) ? c.getString("user_father_dob") : null;
                        String fathername= (c.has("father_name")) ? c.getString("father_name") : null;
                        String ownbbday= (c.has("user_dob")) ? c.getString("user_dob") : null;
                        String ownname= (c.has("user_fname")) ? c.getString("user_fname") : null;
                        occs= (c.has("occ")) ? c.getString("occ") : null;
                        System.out.println("Bday" + wifebday + mothersbday + fatherbday + ownbbday);
                        // if test, to avoid adding null values
                        if(wifebday!=null) statedata.add(wifename+"  :  "+wifebday);
                        if(mothersbday!=null) statedata.add(mothersname+"  :  "+mothersbday);
                        if(fatherbday!=null) statedata.add(fathername+"  :  "+fatherbday);
                        if(ownbbday!=null) statedata.add(ownname+"  :  "+ownbbday);


                        testlist=new ArrayList<String>();
                        if(occs!=null) testlist.add(occs);

                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            } else {
                Log.e("ServiceHandler", "Couldn't get any data from the url");
            }
            return statedata;
        }

        protected void onPostExecute(ArrayList<String> result) {
            super.onPostExecute(result);
            pDialog.dismiss();
            arrallstates = new String[statedata.size()]; // you can also use "result" array instead
            for (int index = 0; index < statedata.size(); index++) {
                arrallstates[index] = statedata.get(index);// or result.get(index);
                //arrallstates[index] =testlist.get(index);
            }
            // pass arrConuntry array to ArrayAdapter<String> constroctor :
            adapterallstates = new ArrayAdapter<String>(
                    WishFriendsFamily.this.getActivity(),
                    android.R.layout.simple_spinner_dropdown_item, arrallstates);
            System.out.println("adpttest" + adapterallstates);
            staticSpinner.setAdapter(adapterallstates);

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

                        String oocs=testlist.get(i).toString();
                    System.out.println("slected ocs" + oocs);
                }
                @Override
                public void onNothingSelected(AdapterView<?> adapterView) {

                }
            });
        }
    }

1 个答案:

答案 0 :(得分:1)

下面:

testlist=new ArrayList<String>(); 
if(occs!=null)    
   testlist.add(occs);

这些行会导致问题,因为testlist每次都在初始化。移动testlist初始化外侧for-loop

jsonObj = new JSONArray(jsonStr);
testlist=new ArrayList<String>();
for (int i = 0; i < jsonObj.length(); i++) {

   //... your code here for json parsing 
   if(occs!=null) 
     testlist.add(occs);
   else
     testlist.add("");
}