2017-06-18 1 views
-1

J'utilise ListView, à l'intérieur de listView j'ai utilisé linearLayout pour remplir les données de cours par JSON.Je veux afficher le Total des cours MarksObtained à travers les données JSON. Je ne suis pas capable d'ajouter les données du JSON et d'afficher dans le champ spécifique.Somme les données JSON et l'affichage dans le champ requis

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; 
    } 
} 

procédé 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" 
    } 
] 

je suppose, je suis en mesure d'ajouter les marques soumises mais quand je l'affichage Somme sur la TextView.The Premier élément ObtainedMarks est affiché plutôt que celle de la somme totale Comment peut-on résoudre ce problème?

+0

Pour être clair: dans votre adaptateur que vous avez la liste student_list_courses d'objets StudentProgressReportPojo. Dans cette classe, il y a un champ SubjectObtainedMarks qui contient un nombre, et vous voulez obtenir la somme de ces nombres dans la liste entière? – thorin86

+0

vous affichez 'SubjectTotal' mais vous ne le définissez que lors de la création du fichier pojo, vous ne mettez pas à jour la valeur si 'examDescription'est présent. droite? – Fusselchen

+0

@ thorin86 exactement – Ghimire

Répondre

1

Peut-être ajouter getter adaptateur:

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

Et puis utilisez studentProgressReportAdapter.getSumOfSubjectObtainedMarks() pour afficher les données appropriées.

Encore une chose: les champs doivent être en minuscules, donc subjectObtainedMarks pas SubjectObtainedMarks etc.

+0

sûr que je vais regarder ça. – Ghimire

+0

s'il vous plaît laissez-moi savoir, vous aurez besoin de plus d'aide – thorin86

+0

je ne suis pas en mesure de résoudre le problème – Ghimire