2017-06-18 69 views
-1

我使用ListView,在listView内我已经使用linearLayout来通过JSON填充课程数据。我想通过JSON数据显示课程MarksObtained的总数。我无法从JSON中添加数据并在特定字段中显示。总结JSON数据和显示在必填字段

StudentProgressReportAdapter

public class StudentProgressReportAdapter extends BaseAdapter { 
    LinearLayout coursesViewDynamic; 

    Context mContext; 
    ArrayList<StudentProgressReportPojo> student_list_courses; 


    String TAG = "HomeTab_adapter"; 

    public StudentProgressReportAdapter(Context mContext, ArrayList<StudentProgressReportPojo> student_list_courses) { 
     super(); 
     this.mContext = mContext; 
     this.student_list_courses = student_list_courses; 
    } 

    @Override 
    public int getCount() { 

     System.out.println(student_list_courses.size()); 
     return student_list_courses.size(); 
    } 

    @Override 
    public Object getItem(int arg0) { 
     return student_list_courses.get(arg0); 
    } 

    @Override 
    public long getItemId(int arg0) { 
     return arg0; 
    } 

    @Override 
    public View getView(final int postion, View convertView, ViewGroup parent) { 
     final StudentProgressReportAdapter.Holder viewHolder; 


     if (convertView == null) { 
      // inflate the layout 
      LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      convertView = inflater.inflate(R.layout.activity_progress_report, parent, false); 


      // well set up the ViewHolder 
      viewHolder = new StudentProgressReportAdapter.Holder(); 

      viewHolder.student_progress_report_termdenoter = (TextView) convertView.findViewById(R.id.progress_term_denoter); 
      viewHolder.student_progress_report_subjectTotal = (TextView) convertView.findViewById(R.id.student_progressreport_subject_total); 



      //added code 
      viewHolder.coursesLayout = (LinearLayout) convertView.findViewById(R.id.courses_layout); 


     } else { 
      // we've just avoided calling findViewById() on resource everytime 
      // just use the viewHolder 
      viewHolder = (StudentProgressReportAdapter.Holder) convertView.getTag(); 
     } 


     // Log.d(TAG, "@@ postion:" + postion + " getFeeDescription" + student_list.get(postion).getFeeDescription()); 
     // Log.d(TAG, "@@ postion:" + postion + " getAmount" + student_list.get(postion).getAmount()); 


     viewHolder.student_progress_report_termdenoter.setText(student_list_courses.get(postion).getTermDenoter()); 
     viewHolder.student_progress_report_subjectTotal.setText(Integer.toString(student_list_courses.get(postion).getSubjectTotal())); 


     // viewHolder.receiptLinearLayout.removeAllViews(); 
     //added code 

     // Fee fee=new Fee(); 
     // JSONArray x=fee.jArray1; 


     viewHolder.coursesLayout.removeAllViews(); 
     for (int i = 0; i < student_list_courses.get(postion).getCourses().size(); i++) { 

      LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      // reciptViewDynamic = (LinearLayout) inflater.inflate(R.layout.layout_bil_info, null); 


      coursesViewDynamic = (LinearLayout) inflater.inflate(R.layout.student_progress_report_courses_listitem, parent, false); 


      // TextView textView = (TextView) coursesViewDynamic.findViewById(R.id.student_progressreport_subject_coursename); 
      // textView.setText(student_list_courses.get(postion).getCourses().get(i)); 
      viewHolder.student_progress_report_courses = (TextView) coursesViewDynamic.findViewById(R.id.student_progressreport_subject_coursename); 
      viewHolder.student_progress_report_courses.setText(student_list_courses.get(postion).getCourses().get(i)); 

      viewHolder.student_progress_report_subjectwise_obtainedmarks = (TextView) coursesViewDynamic.findViewById(R.id.student_progressreport_subject_course_obtainedTerminalmarks); 
      viewHolder.student_progress_report_subjectwise_obtainedmarks.setText(student_list_courses.get(postion).getStudentProgressReportMarksObtained().get(i)); 


      viewHolder.student_progress_report_subjectwise_fullmarks = (TextView) coursesViewDynamic.findViewById(R.id.student_progressreport_subject_terminal_fullmarks); 
      viewHolder.student_progress_report_subjectwise_fullmarks.setText(student_list_courses.get(postion).getStudentProgressReportTerminalFullMarks().get(i)); 


      // Log.d(TAG, "@@ wrong information:" + student_list.get(postion).getFeeDescription()); 
      viewHolder.coursesLayout.addView(coursesViewDynamic); 


     } 

     // (reciptViewDynamic).removeView(convertView); 
     convertView.setTag(viewHolder); 
     return convertView; 
    } 

    class Holder { 
     TextView student_progress_report_courses; 
     TextView student_progress_report_termdenoter; 
     TextView student_progress_report_subjectwise_obtainedmarks; 
     TextView student_progress_report_subjectwise_fullmarks; 
     TextView student_progress_report_subjectTotal; 
     LinearLayout coursesLayout; 
    } 
} 

StudentProgressReportPojo

public class StudentProgressReportPojo { 


    String TermDenoter; 
    String SubjectObtainedMarks; 
    String TerminalFullMarks; 
    Integer SubjectTotal; 


    public StudentProgressReportPojo(String termDenoter, String subjectObtainedMarks, String terminalfullmarks, int total) { 
     TermDenoter = termDenoter; 
     SubjectObtainedMarks = subjectObtainedMarks; 
     TerminalFullMarks = terminalfullmarks; 
     SubjectTotal = total; 

    } 


    public StudentProgressReportPojo(Integer subjectTotal) { 
     SubjectTotal = subjectTotal; 
    } 

    public String getTermDenoter() { 
     return TermDenoter; 
    } 

    public Integer getSubjectTotal() { 
     return SubjectTotal; 
     //SubjectTotal = total; 
    } 

    public void setSubjectTotal(Integer subjectTotal) { 
     SubjectTotal = subjectTotal; 
    } 

    public ArrayList<String> Courses = new ArrayList<String>(); 

    public ArrayList<String> getCourses() { 
     return Courses; 
    } 


    public void setTermDenoter(String termDenoter) { 
     TermDenoter = termDenoter; 
    } 

    public void addCourses(String courses) { 
     Courses.add(courses); 
    } 


    //added 
    public ArrayList<String> StudentProgressReportMarksObtained = new ArrayList<String>(); 

    public ArrayList<String> getStudentProgressReportMarksObtained() { 
     return StudentProgressReportMarksObtained; 
    } 

    public void setStudentProgressReportMarksObtained(ArrayList<String> studentProgressReportMarksObtained) { 
     StudentProgressReportMarksObtained = studentProgressReportMarksObtained; 
    } 

    public String getSubjectObtainedMarks() { 
     return SubjectObtainedMarks; 
    } 

    public void addObtainedMarksSubjectWise(String studentProgressReportMarksObtained) { 
     StudentProgressReportMarksObtained.add(studentProgressReportMarksObtained); 

    } 

    //added 
    public ArrayList<String> StudentProgressReportTerminalFullMarks = new ArrayList<String>(); 

    public ArrayList<String> getStudentProgressReportTerminalFullMarks() { 
     return StudentProgressReportTerminalFullMarks; 
    } 

    public void addTerminalFullMarksSubjectWise(String studentProgressReportTerminalFullMarks) { 
     StudentProgressReportTerminalFullMarks.add(studentProgressReportTerminalFullMarks); 

    } 

    public String getTerminalFullMarks() { 
     return TerminalFullMarks; 
    } 
} 

getUserProgressData方法

public void getUserProgressData() { 


     String URL = Navigation_URL + "?StdID=" + master_id; 
     StringRequest stringRequest = new StringRequest(Request.Method.GET, URL, 
       new Response.Listener<String>() { 
        @Override 
        public void onResponse(String response) { 
         try { 


          ArrayList<StudentProgressReportPojo> student_list_courses = new ArrayList<>(); 


          JSONArray jArray = new JSONArray(response); 
          int x = 0; 
          // studentFeeInformation = new StudentFeeInformation(response); 
          for (int i = 0; i < jArray.length(); i++) { 
           JSONObject jsonObject = jArray.getJSONObject(i); 
           System.out.println(i); 
           String course = jsonObject.getString("CourseName"); 
           String examDescription = jsonObject.getString("examDescription"); 
           String ObtainedMaks = jsonObject.getString("Marks"); 
           // System.out.println("the Obtained Marks is =" + ObtainedMaks); 
           x = (x + Integer.parseInt(ObtainedMaks)); 
           String TerminalFullmarks = jsonObject.getString("Terminal_FM"); 
           if (arrayList.contains(examDescription)) { 
            student_list_courses.get(arrayList.indexOf(examDescription)).addCourses(course); 
            student_list_courses.get(arrayList.indexOf(examDescription)).addObtainedMarksSubjectWise(ObtainedMaks); 
            student_list_courses.get(arrayList.indexOf(examDescription)).addTerminalFullMarksSubjectWise(TerminalFullmarks); 
            //student_list_courses.get(arrayList.indexOf(examDescription)).getSubjectTotal(); 


           } else { 


            StudentProgressReportPojo progressReportPojo = new StudentProgressReportPojo(examDescription, ObtainedMaks, TerminalFullmarks, x); 
            progressReportPojo.addCourses(course); 
            progressReportPojo.addObtainedMarksSubjectWise(ObtainedMaks); 
            progressReportPojo.addTerminalFullMarksSubjectWise(TerminalFullmarks); 
            arrayList.add(examDescription); 
            student_list_courses.add(progressReportPojo); 
            System.out.println("the Total number of x=" + x); 


           } 


           // System.out.println("the Sum=" + ObtainedMaks); 


           // I am going to add the information Within this Section. 
           // StudentProgressReportPojo StudentProgressReportPojo = new StudentProgressReportPojo(x); 
           //StudentProgressReportPojo.getSubjectTotal(x); 
           // StudentProgressReportPojo.setSubjectTotal(x); 

          } 

          System.out.println("Total Marks Obtainedis equal to" + x); 
          //StudentProgressReportPojo progressReportPojo1 = new StudentProgressReportPojo(x); 
          // progressReportPojo1.getSubjectTotal(); 


          System.out.println("student_list size:" + student_list_courses.size()); 
          StudentProgressReportAdapter studentProgressReportAdapter = new StudentProgressReportAdapter(getActivity(), student_list_courses); 
          listView.setAdapter(studentProgressReportAdapter); 


         } catch (JSONException e) { 

          System.out.println("This is not good"); 
          e.printStackTrace(); 

         } 

        } 

       }, new Response.ErrorListener() { 
      @Override 
      public void onErrorResponse(VolleyError error) { 
       // Toast.makeText(view.Fee.this, error.toString(), Toast.LENGTH_LONG).show(); 

      } 
     }) { 

      @Override 
      public Map<String, String> getHeaders() throws AuthFailureError { 
       Map<String, String> headers = new HashMap<String, String>(); 
       return headers; 
      } 

     }; 

     RequestQueue requestQueue = Volley.newRequestQueue(getContext()); 
     requestQueue.add(stringRequest); 


    } 

的Json

[ 
    { 
    "CLASSNO": "1", 
    "CLASS_ID": 2021, 
    "CourseID": 5034, 
    "Marks": 9, 
    "Sno": 1082, 
    "StdID": 95, 
    "TermID": 6014, 
    "CourseName": "Math", 
    "Terminal_FM": 100, 
    "Terminal_PM": 40, 
    "UT_FM": 50, 
    "UT_PM": 20, 
    "examDescription": "First Term", 
    "type": "Terminal", 
    "transferRate": 18, 
    "NAME": "Calvin Patterson" 
    }, 
    { 
    "CLASSNO": "1", 
    "CLASS_ID": 2021, 
    "CourseID": 5035, 
    "Marks": 10.8, 
    "Sno": 1083, 
    "StdID": 95, 
    "TermID": 6014, 
    "CourseName": "English", 
    "Terminal_FM": 100, 
    "Terminal_PM": 40, 
    "UT_FM": 50, 
    "UT_PM": 20, 
    "examDescription": "First Term", 
    "type": "Terminal", 
    "transferRate": 18, 
    "NAME": "Calvin Patterson" 
    } 
] 

我想,我能够添加主题标记,但是当我屏幕上的TextView.The第一项 总和ObtainedMarks的显示而 比总和如何解决这个问题?

+0

只要是明确的:在您的适配器你有StudentProgressReportPojo对象列表student_list_courses。在这个课堂上,有一个领域的SubjectObtainedMarks包含数字,你想获得整个列表中的那些数字的总和? – thorin86

+0

您正在显示'SubjectTotal',但您只在创建pojo时进行设置,如果存在'examDescription',则不会更新值。对? – Fusselchen

+0

@ thorin86正好 – Ghimire

回答

1

也许添加getter到适配器:

public int getSumOfSubjectObtainedMarks(){ 
    int sum; 
    foreach(StudentProgressReportPojo course: student_list_courses){ 
     sum += Integer.parseInt(course.getSubjectObtainedMarks()); 
    } 
    return sum; 
} 

然后用studentProgressReportAdapter.getSumOfSubjectObtainedMarks()来显示正确的数据。

一两件事:字段应该是小写,所以subjectObtainedMarksSubjectObtainedMarks

+0

确定男人我会看看这个。 – Ghimire

+0

请让我知道它,您将需要任何更多的帮助 – thorin86

+0

我无法解决问题 – Ghimire