Anuj
Anuj

Reputation: 129

How to display values comming from json into a list

I m making an Job Search application in which user enters what he is requied and then from the web service i get an json which contains all the info the user needs.Now this info i am storing in an Object.But when i call the object in the list class i get null values.

SearchValues.Java

public class SearchValues {

    public String address,applications,counts,contactInfo,email,expSummary,gender,IjobReqId,area,city,code,country,description,expDate,hours,state,status,title,type,maxExp,minExp,noOfPos,postedon,religion,requestorName,category;
}

SearchJobs.java

 protected void onPostExecute(String s) {
            super.onPostExecute (s);
            SearchValues values = new SearchValues ();
            try {
                jsonObject = new JSONObject (s);
                NewDataSet = jsonObject.getJSONObject ("NewDataSet");
                if (NewDataSet == null) {
                    Toast.makeText (SearchJobs.this, "error", Toast.LENGTH_SHORT).show ();
                } else if (NewDataSet.get ("Table") instanceof JSONObject) {
                    JSONObject table = NewDataSet.getJSONObject ("Table");
                    values.address = table.getString ("Address");
                    values.applications = table.getString ("Applications");
                    values.counts = table.getString ("Candidate_Counts");
                    values.contactInfo = table.getString ("Contact_No");
                    values.email = table.getString ("Email");
                    values.expSummary = table.getString ("Exp_Summary");
                    values.gender = table.getString ("Gender_Name");
                    values.IjobReqId = table.getString ("IJob_Request_ID");
                    values.area = table.getString ("Job_Area");
                    values.category = table.getString ("Job_Category");
                    values.city = table.getString ("Job_City");
                    values.code = table.getString ("Job_Code");
                    values.country = table.getString ("Job_Country");
                    values.description = table.getString ("Job_Desc");
                    values.expDate = table.getString ("Job_Exp_Date");
                    values.hours = table.getString ("Job_Hours");
                    values.state = table.getString ("Job_State");
                    values.status = table.getString ("Job_Status");
                    values.title = table.getString ("Job_Title");
                    values.type = table.getString ("Job_Type");
                    values.maxExp = table.getString ("Max_Exp");
                    values.minExp = table.getString ("Min_Exp");
                    values.noOfPos = table.getString ("No_Of_Pos");
                    values.postedon = table.getString ("Posted_On");
                    values.religion = table.getString ("Religion_Name");
                    values.requestorName = table.getString ("Requestor_Name");

                } else if (NewDataSet.get ("Table") instanceof JSONArray) {
                    JSONArray tablearray = NewDataSet.getJSONArray ("Table");
                    for (int i = 0; i <tablearray.length (); i++) {
                        JSONObject table = tablearray.getJSONObject (i);
                        values.address = table.getString ("Address");
                        values.applications = table.getString ("Applications");
                        values.counts = table.getString ("Candidate_Counts");
                        values.contactInfo = table.getString ("Contact_No");
                        values.email = table.getString ("Email");
                        values.expSummary = table.getString ("Exp_Summary");
                        values.gender = table.getString ("Gender_Name");
                        values.IjobReqId = table.getString ("IJob_Request_ID");
                        values.area = table.getString ("Job_Area");
                        values.category = table.getString ("Job_Category");
                        values.city = table.getString ("Job_City");
                        values.code = table.getString ("Job_Code");
                        values.country = table.getString ("Job_Country");
                        values.description = table.getString ("Job_Desc");
                        values.expDate = table.getString ("Job_Exp_Date");
                        values.hours = table.getString ("Job_Hours");
                        values.state = table.getString ("Job_State");
                        values.status = table.getString ("Job_Status");
                        values.title = table.getString ("Job_Title");
                        values.type = table.getString ("Job_Type");
                        values.maxExp = table.getString ("Max_Exp");
                        values.minExp = table.getString ("Min_Exp");
                        values.noOfPos = table.getString ("No_Of_Pos");
                        values.postedon = table.getString ("Posted_On");
                        values.religion = table.getString ("Religion_Name");
                        values.requestorName = table.getString ("Requestor_Name");

                    }


                }
            } catch (JSONException e) {
                e.printStackTrace ();
            }
            Intent i = new Intent (SearchJobs.this, SearchJobsList.class);
            startActivity (i);
        }
    }

SearchJobsLists.java

public class SearchJobsCustomList extends BaseAdapter {
    Context c;
    SearchValues values=new SearchValues ();

    public SearchJobsCustomList(Context c) {
        super();
        this.c = c;

    }
    @Override
    public int getCount() {
        return 4;
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {

        if(view==null){
            view= LayoutInflater.from (c).inflate (R.layout.custom_search_jobs_lists,viewGroup,false);
            TextView JobCode= (TextView) view.findViewById (R.id.tv_job_code);
            TextView Category= (TextView) view.findViewById (R.id.tv_category);
            TextView ExpYrs= (TextView) view.findViewById (R.id.tv_exp_yrs);
            TextView ExpMnths= (TextView) view.findViewById (R.id.tv_exp_mnths);
            TextView Date= (TextView) view.findViewById (R.id.tv_date);


            JobCode.setText (values.code);
            Category.setText (values.category);
            ExpYrs.setText (values.minExp);
            ExpMnths.setText (values.maxExp);
            Date.setText (values.postedon);





        }
        return view;
    }
}

Json Response

 { "NewDataSet": { "Table": [ {"Address": "Kurla,Mumbai,Maharashtra,INDIA", "Applications": "2", "Candidate_Counts": "0", "Contact_No": "9896969696", "Email": "[email protected]", "Exp_Summary": "test", "Gender_Name": "Male", "IJob_Request_ID": "1", "Job_Area": "Kurla", "Job_Category": "Caretaker", "Job_City": "Mumbai", "Job_Code": "J-111", "Job_Country": "INDIA", "Job_Desc": "Caretaker", "Job_Exp_Date": "27 Jun 2014", "Job_Hours": "8.00", "Job_State": "Maharashtra", "Job_Status": "Open", "Job_Title": "Caretaker", "Job_Type": "Permanent", "Max_Exp": "0.60", "Min_Exp": "0.00", "No_Of_Pos": "5", "Posted_On": "13 Jun 2014", "Religion_Name": "Hindu", "Requestor_Name": "sagar shinde" }, {"Address": "Chembur,Mumbai,Maharashtra,INDIA", "Applications": "0", "Candidate_Counts": "0", "Contact_No": "9896969696", "Email": "[email protected]", "Exp_Summary": "test", "Gender_Name": "Male", "IJob_Request_ID": "2", "Job_Area": "Chembur", "Job_Category": "Caretaker", "Job_City": "Mumbai", "Job_Code": "j-112", "Job_Country": "INDIA", "Job_Desc": "need Caretaker", "Job_Exp_Date": "27 Jun 2014", "Job_Hours": "8.00", "Job_State": "Maharashtra", "Job_Status": "Open", "Job_Title": "Caretaker", "Job_Type": "Permanent", "Max_Exp": "0.60", "Min_Exp": "0.00", "No_Of_Pos": "5", "Posted_On": "13 Jun 2014", "Religion_Name": "Hindu", "Requestor_Name": "sagar shinde" } ] }}

Upvotes: 0

Views: 91

Answers (2)

Android
Android

Reputation: 398

I am not sure what the problem is.. I am assuming that may be the problem with setting the data.. Here I am giving the sample what i used

 public class StageArray {

    private JSONArray data = new JSONArray();

   public JSONArray getData() {
       return data;
       }
   public void setData(JSONArray data) {
       this.data = data;
       }

   private static final StageArray holder = new StageArray();
   public static StageArray getInstance() {
       return holder;
       }
   }

When ever I am set the data i will call

StageArray.getInstance().setData(JSONObject );

for getting

stageArray = StageArray.getInstance().getData();

Try by this... Hope it will work

EDIT:

I worked around this and GOT it WORKED I updated StageArray code check it before going to next lines

In else if condition changed to

else if (NewDataSet.get ("Table") instanceof JSONArray) {
                JSONArray tablearray = NewDataSet.getJSONArray ("Table");
                StageArray.getInstance().setData(tablearray);
             }

And Change the getView Function

 @Override
    public View getView(int i, View view, ViewGroup viewGroup) {

        if(view==null){
            view= LayoutInflater.from (c).inflate (R.layout.custom_search_jobs_lists,viewGroup,false);

            TextView Category= (TextView) view.findViewById (R.id.tv_category);
            TextView ExpYrs= (TextView) view.findViewById (R.id.tv_exp_yrs);
            TextView ExpMnths= (TextView) view.findViewById (R.id.tv_exp_mnths);
            TextView Date= (TextView) view.findViewById (R.id.tv_date);

            JSONArray stageArray = StageArray.getInstance().getData();

            for (int j = 0; j < stageArray.length(); j++) {
                 try {
                    JSONObject c = stageArray.getJSONObject(j);
                     Category.setText (c.getString("Job_Category"));
                      ExpYrs.setText (c.getString("Min_Exp"));
                      ExpMnths.setText (c.getString("Max_Exp"));
                      Date.setText (c.getString("Posted_On"));
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }
            }
        return view;
    }

If it works.. just approve this ans.

Upvotes: 1

zerob13
zerob13

Reputation: 11

Because I don't know your data,I'm not sure what the problem is...In my opinion,may be some of your data is empty. For example, somebody don't have an address.In this case:

values.address = table.getString ("Address");

This code will throw a JSONException,and values will be null.So I suggest you to use optString (String name) :

values.address = table.optString ("Address");

The same as above,replace all JSONObject.getXXXX() to JSONOBject.optXXXX() may help.

Upvotes: 1

Related Questions