2014-06-18 16 views
0

我做了一個工作搜索應用程序,在其中用戶輸入他所需的,然後從Web服務我得到一個JSON,其中包含用戶需要的所有信息。現在這個信息我存儲在一個Object.But當我調用列表類中的對象時,我得到空值。如何顯示從json到列表中的值

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響應

{ "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" } ] }} 
+0

這與你的問題無關,但使用GSON將使從Json到對象的轉換變得更容易。 –

回答

1

我不知道是什麼問題..我假設當過我設定的數據可能不符合設置數據.. 在這裏,我給我用什麼

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

樣本問題我會打電話給

StageArray.getInstance().setData(JSONObject); 

通過此獲得

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

嘗試...希望它會工作

編輯:

我工作圍繞這一點,GOT它的工作 我更新StageArray代碼之前要下一行

在否則,如果條件改變爲

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

並更改getView功能

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

如果它工作..只是批准這個答案。

+0

不工作 – Anuj

+0

你能顯示json響應嗎 – Android

+0

我編輯了我的文章plz hav一看 – Anuj

1

因爲我不知道你的數據,我不確定是什麼問題......在我看來,可能有些數據是空的。例如,有人沒有一個address.In這種情況下:

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

此代碼將拋出一個JSONException,和值將是null.So我建議你使用optString (String name)

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

與上面相同,將所有JSONObject.getXXXX()替換爲JSONOBject.optXXXX()可能會有所幫助。

+0

我的json得到正確解析,並且信息geting存儲在值bt當我在列表視圖中調用它時我沒有得到anuthing – Anuj