我正在尝试开发一个 android 移动应用程序,我需要在其中显示至少有 100 个项目的列表视图的一部分。现在我,作为一所大学的管理员,我列出了 100 个要教授的科目。(所有科目都列在一个列表视图中。所有科目都有一个点,为了让学生申请这门课程,他/她需要有更高的分数。一个指示性样本如下: 英语:24 西班牙语:16 数学:28 科学:26 法语:16 管理:22 法律:30 亚洲语言:10
现在学生需要输入他/她的分数并基于(精确或低分),他/她得到他/她有资格申请的科目列表。
例如输入您的积分:24
列表视图中的输出应给出以下内容:法语:16 管理:22 英语:24 亚洲语言:10
我试过了,但卡住了,无法完成代码:
Course.java
public class Course {
private String name;
private int points;
public Course(String name, int points) {
this.name = name;
this.points = points;
}
public String getName() {
return name;
}
public int getPoints() {
return points;
}
@Override
public String toString() {
return getName();
}
}
Course[] courses = new Course[]{
new Course("Medicine", 30),
new Course("Physics", 28),
new Course("Math", 24),
new Course("English", 20)
};
Button searchButton = (Button) findViewById(R.id.btn_search);
searchButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
List<Course> courseList = new ArrayList<Course>(Arrays.asList(courses));
// initialize the adapter with courseList... //
// this code inside the Button's onClick
int points = Integer.parseInt(editText.getText().toString());
for (Course c : courses) {
if (c.getPoints() <= points) {
adapter.add(c);
}
}
course.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:layout_width="0dp"
android:layout_weight="1"
android:maxLines="1"
android:inputType="number"
android:layout_height="wrap_content"
android:hint="Enter your points:"
android:id="@+id/editText"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Search"
android:id="@+id/btn_search"/>
</LinearLayout>
<ListView
android:id="@+id/courseNames"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>