android - 数据覆盖上一个

标签 android json

这是我的接口(interface)

[  
   {  
      "id":1,
      "name":"Little Angels Higher Secondary School",
      "address":"Hattiban",
      "district":"Lalitpur",
      "country":"Nepal",
      "phone":"01-41111111",
      "email":"lac@gmail.com",
      "website":"wwww.la.com.np",
      "institution_type":"Government",
      "establishment_date":"15-12-2016",
      "admission_open_from":"15-12-2016",
      "admission_open_to":"19-12-2016",
      "logo":"http:\/\/www.myeducationhunt.com\/logos\/logo1.png",
      "active":0,
      "created_at":null,
      "updated_at":null,
      "latitude":27.6514,
      "longitude":85.3359,
      "fees":[  
         {  
            "id":1,
            "grade":"1",
            "price":12000,
            "school_id":1,
            "created_at":null,
            "updated_at":null
         },
         {  
            "id":2,
            "grade":"2",
            "price":13000,
            "school_id":1,
            "created_at":null,
            "updated_at":null
         }
      ]
   },
   {  
      "id":6,
      "name":"Baltimore Secondary School",
      "address":"Baltimore",
      "district":"idk",
      "country":"U.S",
      "phone":"1111111111111",
      "email":"bat@gmail.com",
      "website":"www.baltimore.edu.np",
      "institution_type":"Private",
      "establishment_date":"15-12-2016",
      "admission_open_from":"15-12-2016",
      "admission_open_to":"20-12-2016",
      "logo":"http:\/\/www.myeducationhunt.com\/logos\/logo2.png",
      "active":0,
      "created_at":null,
      "updated_at":null,
      "latitude":27.6514,
      "longitude":85.3359,
      "fees":[  
         {  
            "id":9,
            "grade":"1",
            "price":12000,
            "school_id":6,
            "created_at":null,
            "updated_at":null
         },
         {  
            "id":10,
            "grade":"2",
            "price":12500,
            "school_id":6,
            "created_at":null,
            "updated_at":null
         },
         {  
            "id":11,
            "grade":"3",
            "price":15000,
            "school_id":6,
            "created_at":null,
            "updated_at":null
         },
         {  
            "id":12,
            "grade":"4",
            "price":19000,
            "school_id":6,
            "created_at":null,
            "updated_at":null
         }
      ]
   }]

我在学校类里面将这些数据解析为:

JsonArrayRequest schoolRequest = new JsonArrayRequest(url,
                new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(JSONArray response) {
                        Log.d(TAG, response.toString());
                        hidePDialog();

                        // Parsing json
                        for (int i = 0; i < response.length(); i++) {
                            try {

                                JSONObject obj = response.getJSONObject(i);
                                OurSchool ourSchool = new OurSchool();

                                ourSchool.schoolId = obj.getInt("id");
                                ourSchool.schoolName = obj.getString("name");
                                ourSchool.schoolLocation = obj.getString("address");
                                ourSchool.schoolLogo = obj.getString("logo");
                                ourSchool.district=obj.getString("district");
                                ourSchool.country=obj.getString("country");
                                ourSchool.phone=obj.getString("phone");
                                ourSchool.email=obj.getString("email");
                                ourSchool.website=obj.getString("website");
                                ourSchool.type=obj.getString("institution_type");
                                ourSchool.estb_date=obj.getString("establishment_date");
                                ourSchool.admission_start_date=obj.getString("admission_open_from");
                                ourSchool.admission_end_date=obj.getString("admission_open_to");
                                ourSchool.latitude=obj.getDouble("latitude");
                                ourSchool.longitude=obj.getDouble("longitude");
                                // adding schools to ourSchool list

                                JSONArray fees=obj.getJSONArray("fees");
                                List<FeeClass> listFeeClass= new ArrayList<FeeClass>();//by Ahamed
                                for (int j=0;j<fees.length(); j++){
                                    FeeClass feeClass= new FeeClass();//by Ahamed
                                    JSONObject obj1=fees.getJSONObject(j);
                                       feeClass.setGrade(obj1.getString("grade"));//By Ahamed
                                    feeClass.setFee(obj1.getString("price"));//ByAhamed

                                   listFeeClass.add(feeClass);//by Ahamed

                                    ourSchool.grade=obj1.getString("grade");//you need to remove this line
                                    ourSchool.fee=obj1.getString("price");//you need to remove this line
                                }
                                ourSchool.setFeesList(listFeeClass);//by Ahamed
                                ourSchoolsListItems.add(ourSchool);

                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }

                        adapter.notifyDataSetChanged();
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                VolleyLog.d(TAG, "Error: " + error.getMessage());
                hidePDialog();
            }
        });

我将这些数据发送到我的 schooldetail 类中作为

Intent i = new Intent(School.this, SchoolDetails.class);

                i.putExtra("school", ourSchoolsListItems.get(position));
                startActivity(i);

最后我尝试检索相应学校的所有这些数据

 ourSchool = (OurSchool) getActivity().getIntent().getSerializableExtra("school");

一切正常。唯一的问题是 api 中的“费用”对象。我想显示相应类(class)的所有费用明细。但我只是在“费用”中获取最后一个对象的费用详细信息。例如在小天使的学费数组中,我只是获取 2 年级的 ID、年级和价格。我想检索费用数组中的所有对象详细信息。请帮忙。

更新 //通过艾哈迈德

 class FeeClass{
       String grade,fee;
    public void setGrade(String grade){
      this.grade=grade;
    }
    public String getGrade(){
      return this.grade;
    }
 }

在您的 OurSchool 类中添加以下变量。

    List<FeeClass> feeClassObject= new ArrayList<FeeClass>();

还有 getter 和 setter

    public void setFeesList(List<FeeClass> feeClassObject){
      this.feeClassObject=feeClassObject;
    }
    public List<FeeClass> getFeesList(){
      return  feeClassObject;
    }

然后查看您的 for 循环代码

最佳答案

因为,您的第一个 for 循环学校创建了一个 OurSchool 对象。在那个单一的 ourschool 对象中,您试图将所有费用详细信息放在单个指针对象中。因此,第一个费用明细保存在该费用明细对象中,第二个费用明细再次替换您现有的费用明细对象。在你的第二个 for 循环完成之前做同样的事情。因此,最后一项只会存在于该对象上。就这样。快乐编码。

编辑

因此,您必须在我们的学校对象上维护一个列表。

喜欢,

class FeeObject{
 String feeDetails1,feeDetails2..etc
}

在 OurSchoolObject 中添加以下变量。

  List<FeeObject> feeObjectList= new ArrayList<>();

就这样

关于android - 数据覆盖上一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41215865/

相关文章:

python - 从 pandas 的 JSON 列中提取属于特定键的值

java - com.google.gson.internal.LinkedTreeMap 无法转换为我的类(class)

ios - 如何检查 connectionDidFinishLoading 何时完成

android - Google 结算不适用于调试版本的 applicationIdSuffix ".debug"

android - 在 firebase 中使用名称、用户名等创建新用户

java - 基于HTTP请求的Android聊天

javascript - 使用 jquery 读取 json

jquery - 在 jsTree 中,我得到 data.rslt 为未定义

android - 如何改进 Android 开发者文档?

Android 标签栏 View 重叠?