我做了一個工作搜索應用程序,在其中用戶輸入他所需的,然後從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" } ] }}
這與你的問題無關,但使用GSON將使從Json到對象的轉換變得更容易。 –