gpt4 book ai didi

java - 如何在 ListView 中显示带有数据数组的对象

转载 作者:行者123 更新时间:2023-12-01 08:59:38 25 4
gpt4 key购买 nike

有以下对象:

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

类(class)包含作业对象:

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

数据必须在 ListView 中显示为:

enter image description here

我正在关注本教程:https://www.tutorialspoint.com/android/android_list_view.htm 。接下来,该 Activity 有一个字符串数组,创建一个适配器并将其链接到一个 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>() ? ListView 中是否需要垂直布局?

<小时/>

更新:可能的布局文件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>

可能的分配布局文件copy_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>

最佳答案

您的类(class)将是标题类型,作业将是 ListView 中行的部分类型。然后您可以在ListView中显示列表。对于您的实现帮助,您可以查看这个简单的实现---http://stacktips.com/tutorials/android/listview-with-section-header-in-android

关于java - 如何在 ListView 中显示带有数据数组的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41786492/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com