Ghimire Ghimire -3 years ago 44
Android Question

Display inner Items under Certain List Item?

There is a ListView within which I have used a Dynamic Linear Layout to add inner elements. I want to display all the Specific Term wise inner data within a specific list (i.e. Only Specific Term Name to be displayed and all the information within the TermName). However, I am getting the inner item as Another List Item, i.e is not what I am expecting.

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


}


I want Specific Term under which the all Courses to be Displayed.

Can't I Display all the Courses under the same Specific Term Name?

Answer Source

Change your for loop to add similar items to same object , change it to

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

    }

also you need to change

            viewHolder.student_progress_report_courses
.setText(student_list_courses.get(postion).getCourses().get(i));

in your getView() function to display all Courses not just getCourses().get(i)) you need to add textviews for each item getCourses returns.

something like this

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

Hope this helps.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download