我正在创建一个Job Search应用程序,用户输入他所要求的内容,然后从Web服务我得到一个json,其中包含用户需要的所有信息。现在这个信息我存储在一个Object.But当我调用列表类中的对象,我得到空值。
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": "sagar@gmail.com", "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": "sagar@gmail.com", "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 :(得分: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 it WORKED 我更新了 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;
}
如果有效..只要批准这个ans。
答案 1 :(得分:1)
因为我不知道您的数据,我不确定问题是什么......在我看来,您的某些数据可能是空的。例如,某人没有地址。在这种情况下:
values.address = table.getString ("Address");
此代码将抛出JSONException,值将为null。所以我建议您使用optString (String name):
values.address = table.optString ("Address");
与上面相同,将所有JSONObject.getXXXX()替换为JSONOBject.optXXXX()可能有所帮助。