0

there is the following objects:

public class Course {
    private static int courseID = 0; 
    private String courseTitle; 
    private ArrayList<Assignment> assignments;
    private Course(String title, ArrayList<Assignment> assns)
    {
        courseTitle = title;
        assignments = assns;
        courseID++;
    }
    //returns a Course instant with random assignment values
    static public Course generateRandomCourse()
    {
        Random rnd = new Random();
        int assignmentNo = rnd.nextInt(5);
        ArrayList<Assignment> tempAssns = new ArrayList<Assignment>();
        for(int i=0; i < assignmentNo; i++)
            tempAssns.add(Assignment.generateRandomAssignment());
        return new Course("Course " + courseID, tempAssns);
    }
    public String getCourseTitle() {return courseTitle;}
    public ArrayList<Assignment> getAssignments() {return assignments;}
}

Course contains Assignment objects:

public class Assignment {
    private static int assID = 0; //static ID increments with every new
    private String assignmentTitle; //title of assignment
    private int assignmentGrade; //grade of assignment
    //private constructor. Increments ID.
    private Assignment(String title, int grade)
    {
        assignmentTitle = title;
        assignmentGrade = grade;
        assID++;
    }
    //returns an Assignment instance with random values
    static public Assignment generateRandomAssignment()
    {
        Random rnd = new Random();
        String tempTitle = "Assignment " + assID;
        int tempGrade = rnd.nextInt(100) + 1;
        return new Assignment(tempTitle, tempGrade);
    }
    public String getAssignmentTitle() {return assignmentTitle; }
    public int getAssignmentGrade() {return assignmentGrade;}
}

The data must be displayed in a list view as:

enter image description here

I am following this tutorial:https://www.tutorialspoint.com/android/android_list_view.htm. Following it, the activity has an array of strings, creates an adapter and links it to a Listwiew:

String[] mobileArray = {"Android","IPhone","WindowsMobile","Blackberry",
  "WebOS","Ubuntu","Windows7","Max OS X"};

ArrayAdapter adapter = new ArrayAdapter<String>(this, 
     R.layout.activity_listview, mobileArray);

listView listView = (ListView) findViewById(R.id.mobile_list);
  listView.setAdapter(adapter);

The xml file contains:

   <ListView
      android:id="@+id/mobile_list"
      android:layout_width="match_parent"
      android:layout_height="wrap_content" >
   </ListView>

The activity_listview.xml file contains:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/label"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dip"
    android:textSize="16dip"
    android:textStyle="bold" >
</TextView>

How to get Listview from: ArrayList<Course>courses = new ArrayList<Course>()? Is a vertical layout within the listview needed?


Update: Possible layout file course_row_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/coureTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"/>
    <ListView
        android:id="@+id/assignments"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>   
</LinearLayout>

possible assignment layout file assignment_row_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/assignmentTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"/>
    <TextView
        android:id="@+id/cassignmentGrade"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"/>        
</LinearLayout>
3
  • Use custom adapter stackoverflow.com/questions/8166497/… Commented Jan 22, 2017 at 2:04
  • @AnixPasBesoin I am unsure how to mange the ArrayList that are part of the object. I updated the post with an attempt at the layout files, but I am lost with ListAdapter. Commented Jan 22, 2017 at 4:21
  • Just look for customAdapter in the internet, there are many good tutorials out there. Commented Jan 22, 2017 at 4:23

1 Answer 1

1

Your Course will be header type and Assignment will be section type of row in ListView . Then you can show the list in ListView. For your implementation help you can see this simple implementation--- http://stacktips.com/tutorials/android/listview-with-section-header-in-android

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.