Android 如何显示从json提交到列表中的值

Android 如何显示从json提交到列表中的值,android,json,listview,Android,Json,Listview,我正在制作一个求职应用程序,用户在其中输入他需要的内容,然后从web服务中我得到一个json,其中包含用户需要的所有信息。现在,我将这些信息存储在一个对象中。但是当我调用list类中的对象时,我会得到空值 SearchValues.Java public class SearchValues { public String address,applications,counts,contactInfo,email,expSummary,gender,IjobReqId,area,cit

我正在制作一个求职应用程序,用户在其中输入他需要的内容,然后从web服务中我得到一个json,其中包含用户需要的所有信息。现在,我将这些信息存储在一个对象中。但是当我调用list类中的对象时,我会得到空值

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);
        }
    }
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" } ] }}

我不确定是什么问题。。我想这可能是设置数据的问题。。 在这里,我给出了我使用的样本

 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();
试试这个。。。希望它能起作用

编辑:

我解决了这个问题,并且成功了 我更新了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;
    }
@覆盖
公共视图getView(int i、视图视图、视图组视图组){
如果(视图==null){
view=LayoutFlater.from(c).充气(R.layout.custom\u search\u jobs\u List,viewGroup,false);
TextView类别=(TextView)view.findViewById(R.id.tv_类别);
TextView ExpYrs=(TextView)view.findViewById(R.id.tv\u exp\u yrs);
TextView ExpMnths=(TextView)view.findViewById(R.id.tv\u exp\u mnths);
TextView日期=(TextView)view.findViewById(R.id.tv_日期);
JSONArray stagerray=stagerray.getInstance().getData();
对于(int j=0;j

如果行得通。。请批准此ans。

因为我不知道您的数据,我不确定问题出在哪里……在我看来,可能是您的一些数据是空的。例如,某人没有地址。在本例中:

values.address = table.getString ("Address");
此代码将抛出JSONException,值将为null。因此,我建议您使用:


如上所述,将所有JSONObject.getXXXX()替换为JSONObject.optXXXX()可能会有所帮助。

这与您的问题无关,但是使用GSON将使从Json到对象的转换更加容易。我的Json被正确解析,而且当我在列表视图中调用它时,信息被存储在值bt中,我没有得到它anuthing@user3667909是否在SearchJobsList中使用SearchJobsCustomList?@user3667909是seams数据就像数组一样。。因此,每次迭代都会增加值,但在获取值时,您并没有使用所需数据的位置。。因此,它忽略了位置的概念。。检查一次全流量
values.address = table.getString ("Address");
values.address = table.optString ("Address");