有以下对象:
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;}
}
课程包含作业对象:
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;}
}
数据必须在列表视图中显示为:
我正在关注本教程:https://www.tutorialspoint.com/android/android_list_view.htm。 在它之后,活动有一个字符串数组,创建一个适配器并将其链接到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);
xml文件包含:
<ListView
android:id="@+id/mobile_list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
activity_listview.xml文件包含:
<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>
如何从Listview获取:
ArrayList<Course>courses = new ArrayList<Course>()
?
需要列表视图中的垂直布局吗?
更新: 可能的布局文件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>
可能的分配布局文件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>
答案 0 :(得分:1)
您的课程将是标题类型,而分配将是 ListView 中的部分行类型。然后,您可以在 ListView 中显示列表。对于您的实施帮助,您可以看到这个简单的实现--- http://stacktips.com/tutorials/android/listview-with-section-header-in-android