2017-06-18 66 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