albert
albert

Reputation: 251

How to get json object as per spinner selection

I am getting name and date in my spinner from this response,now what i am trying is if user select lily from spinner then i want to get occ value in string,but i am not getting it,following is my response and code,can any one tell me that what is the issue

[
    {
        "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) {

                }
            });
        }
    }

Upvotes: 0

Views: 262

Answers (1)

ρяσѕρєя K
ρяσѕρєя K

Reputation: 132982

Here:

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

these lines causing issue, because testlist is initializing every time. move testlist initialization out-side 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("");
}

Upvotes: 1

Related Questions