在某些列表项下显示内部项?

时间:2017-06-14 04:52:09

标签: android listview android-linearlayout

有一个ListView,我在其中使用动态线性布局来添加内部元素。我想在特定列表中显示所有特定术语明智的内部数据(即,仅显示特定术语名称和TermName中的所有信息)。但是,我将内部项目作为另一个列表项目,即不是我所期待的。

JSON

[
  {
    "CLASSNO": "1",
    "CLASS_ID": 2021,
    "CourseID": 4032,
    "Marks": 45,
    "Sno": 35,
    "StdID": 95,
    "TermID": 6014,
    "CourseName": "History",
    "Terminal_FM": 50,
    "Terminal_PM": 20,
    "UT_FM": 100,
    "UT_PM": 40,
    "examDescription": "First Term",
    "type": "Terminal",
    "NAME": "Calvin Patterson"
  },
  {
    "CLASSNO": "1",
    "CLASS_ID": 2021,
    "CourseID": 4033,
    "Marks": 35,
    "Sno": 36,
    "StdID": 95,
    "TermID": 6014,
    "CourseName": "Science",
    "Terminal_FM": 50,
    "Terminal_PM": 20,
    "UT_FM": 100,
    "UT_PM": 40,
    "examDescription": "First Term",
    "type": "Terminal",
    "NAME": "Calvin Patterson"
  },
  {
    "CLASSNO": "1",
    "CLASS_ID": 2021,
    "CourseID": 4032,
    "Marks": 45,
    "Sno": 37,
    "StdID": 95,
    "TermID": 6015,
    "CourseName": "History",
    "Terminal_FM": 50,
    "Terminal_PM": 20,
    "UT_FM": 100,
    "UT_PM": 40,
    "examDescription": "Second Term",
    "type": "Terminal",
    "NAME": "Calvin Patterson"
  },
  {
    "CLASSNO": "1",
    "CLASS_ID": 2021,
    "CourseID": 4033,
    "Marks": 30,
    "Sno": 38,
    "StdID": 95,
    "TermID": 6015,
    "CourseName": "Science",
    "Terminal_FM": 50,
    "Terminal_PM": 20,
    "UT_FM": 100,
    "UT_PM": 40,
    "examDescription": "Second Term",
    "type": "Terminal",
    "NAME": "Calvin Patterson"
  }
]

StudentProgressReportPojo

public class StudentProgressReportPojo {


    String TermDenoter;


    public StudentProgressReportPojo(String termDenoter) {
        TermDenoter = termDenoter;
    }

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

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

    public void setCourses(ArrayList<String> courses) {
        Courses = courses;
    }

    public String getTermDenoter() {
        return TermDenoter;
    }

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

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

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


            //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.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(Integer.parseInt(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));


            // 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;
        LinearLayout coursesLayout;
    }
}

ProgressFragment

public class ProgressFragment extends Fragment {

    ListView listView;
    String master_id;
    String Navigation_URL = "http://192.168.100.5:84/api/academics/getSingleStudentsMarks";
    ArrayList arrayList = new ArrayList();


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.student_progressreport_listview, container, false);
        setHasOptionsMenu(true);


        SessionManagement sessionManagement = new SessionManagement(getContext());
        master_id = sessionManagement.getMasterId();

        listView = (ListView) view.findViewById(R.id.list_student_progress_report);


        getUserProgressData();

        return view;
    }


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

                            //  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");
                                // progressReportPojo.setCourses(course);
                                // progressReportPojo.getCourses();
                                StudentProgressReportPojo progressReportPojo = new StudentProgressReportPojo(examDescription);
                                arrayList.add(examDescription);

                                //   arrayList.add(course);
                                System.out.println("course" + arrayList);
                                progressReportPojo.addCourses(course);

                                // progressReportPojo.getCourses(course);
                                student_list_courses.add(progressReportPojo);


                            }

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


    }


    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        // TODO Auto-generated method stub
        super.onCreateOptionsMenu(menu, inflater);
        inflater.inflate(R.menu.dashboard, menu);
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // handle item selection
        switch (item.getItemId()) {
            case R.id.action_settings:
                // do s.th.
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }


}

我想要显示所有课程的特定术语。

我不能在相同的特定术语名称下显示所有课程吗?

2 个答案:

答案 0 :(得分:1)

更改for循环,将类似的项添加到同一个对象 , 将其更改为

        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");
        if(arrayList.contains(examDescription))) {
            student_list_courses.get(arrayList
                    .indexOf(examDescription)).addCourses(course);
        }
        else{
            StudentProgressReportPojo progressReportPojo = new StudentProgressReportPojo(examDescription);
            progressReportPojo.addCourses(course);
            arrayList.add(examDescription);
            student_list_courses.add(progressReportPojo);
        }

    }

你也需要改变

            viewHolder.student_progress_report_courses
.setText(student_list_courses.get(postion).getCourses().get(i));
在你的getView()函数中

显示所有课程而不仅仅是getCourses().get(i)) 您需要为getCourses返回的每个项目添加textview。

类似这样的事情

for(int x=0;x<student_list_courses.get(postion).getCourses().size();x++){
LinearLayout 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.coursesLayout.addView(coursesViewDynamic);
}

希望这有帮助。

答案 1 :(得分:0)

您可以使用GSON / Jackson来解析您的JSON对象。你会发现很多注释可以在这些库中排除EMPTY,NULL等。它们还提供了更容易阅读的代码

此处给出了使用GSON的示例

https://www.mkyong.com/java/how-do-convert-java-object-to-from-json-format-gson-api/

相关问题