如何过滤 Android 中的对象列表
How to filter a List of objects in Android
我正在尝试创建一个 android 应用程序(新的)!
我的问题如下:
假设我是X某大学的管理员
我需要列出每门课程都有不同的要点。
例如:
- 医学:30
- 物理学:28
- 数学:24
- 英语:20
现在假设一个学生输入点28。
我的应用程序应该能够检测到具有 28 分的课程以及较低分的课程。那是;物理、数学和英语但不是医学!!!
谁能帮我解决这个问题?我到处都找遍了,还没有结果!
拜托大家很难做到这一点!谢谢!
MainActivity.java
import android.os.Bundle;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
public class MainActivity extends Activity {
String[] courses = new String[]{
"Mathematics",
"Chemical Engineering",
"Civil Engineering",
"Agriculture",
"Law and Management",
"Sociology",
"Information and Communication Technologies",
"Computer Science",
"Information Systems",
"Software Engineering",
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.event2icon);
EditText editText = null;
editText=(EditText)findViewById(R.id.edit_text);
final String point=editText.getText().toString();
final int point_integer=Integer.parseInt(point);
final ListView courselist = (ListView)findViewById(R.id.courseNames);
LayoutInflater inflater = getLayoutInflater();
ViewGroup header = (ViewGroup)inflater.inflate(R.layout.listviewheader, courselist, false);
courselist.addHeaderView(header, null, false);
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, courses);
courselist.setAdapter(adapter);
courselist.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// TODO Auto-generated method stub
//we use the items of the listview as title of the next activity
if((Integer.parseInt(String.valueOf(point_integer))<=10))
{
}
}
});
}
}
event2icon.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">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/edit_text"
android:text="Enter your points:"/>
<ListView
android:id="@+id/courseNames"
android:layout_width="match_parent"
android:layout_below="@+id/edit_text"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:dividerHeight="15.0sp"
>
</ListView>
</LinearLayout>
listviewheader.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>
您没有找到任何东西,因为您需要分解问题并逐一研究:
你想要一个代表课程的 class,带有名称和分数
字段。
包含这些课程实例的列表(如您所列)。
您需要一个 activity,其中包含供学生输入的文本框
数入.
您需要将文本解析为整数。
您需要迭代您定义的列表。
并且通过使用小于或等于运算符 <=
,您会发现
您需要的课程。
在 Android 你应该使用
ListAdapter
很好地显示列表。
你应该能够执行 weston 在他的回答中提到的所有步骤,否则,恐怕我只给你代码不会教你太多。
相反,我会为您提供其他答案中提到的片段。
You want a class that represents the course, with name and points fields
这是一个简单的模型class。 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();
}
}
A list that contains the instances of these courses
你只有字符串,没有在你的代码中列出点数,你怎么会想到用数字来过滤课程呢?这是您作为数组列出的示例课程的示例。
Course[] courses = new Course[]{
new Course("Medicine", 30),
new Course("Physics", 28),
new Course("Math", 24),
new Course("English", 20)
};
You need an activity that includes a text box for student to enter number into
这需要您写一些 XML,您已经在 event2icon.xml
中完成了。我建议您添加一个 Button
,因为在 EditText
中按 Enter 会插入一个换行符。您也可以在创建时只接受数字。这是我建议的完整 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>
You need to parse the text to an integer
你知道怎么做 - 但问题是你什么时候做这个?您发布的代码会在 Activity 加载后立即从 EditText
中获取文本,这将是空的。当您单击按钮时,您应该改为从 EditText
中提取文本。这会让你开始。
Button searchButton = (Button) findViewById(R.id.btn_search);
searchButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
You need to iterate over list you defined. And by using the less than or equal operator <=, you'll find the courses you need
这只是对您定义的 Courses 数组的简单 for-each 循环。找到 Course 对象后,将其添加到适配器中。您还应该知道,您需要将 Course[]
转换为 List<Course>
才能执行此操作,否则会出现错误。
List<Course> courseList = new ArrayList<Course>(Arrays.asList(courses));
// initialize the adapter with courseList instead of courses
final ArrayAdapter<Course> adapter = new ArrayAdapter<Course>(this, android.R.layout.simple_list_item_1, 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);
}
}
Use a ListAdapter to display the list nicely
你已经有一个 ArrayAdapter
,所以你在这里很好。不过,最后一个细节是,当您单击 Button
时,您应该先 clear
适配器,然后再将课程重新添加到其中。否则,你会得到重复的。
我正在尝试创建一个 android 应用程序(新的)!
我的问题如下:
假设我是X某大学的管理员
我需要列出每门课程都有不同的要点。
例如:
- 医学:30
- 物理学:28
- 数学:24
- 英语:20
现在假设一个学生输入点28。
我的应用程序应该能够检测到具有 28 分的课程以及较低分的课程。那是;物理、数学和英语但不是医学!!!
谁能帮我解决这个问题?我到处都找遍了,还没有结果!
拜托大家很难做到这一点!谢谢!
MainActivity.java
import android.os.Bundle;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
public class MainActivity extends Activity {
String[] courses = new String[]{
"Mathematics",
"Chemical Engineering",
"Civil Engineering",
"Agriculture",
"Law and Management",
"Sociology",
"Information and Communication Technologies",
"Computer Science",
"Information Systems",
"Software Engineering",
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.event2icon);
EditText editText = null;
editText=(EditText)findViewById(R.id.edit_text);
final String point=editText.getText().toString();
final int point_integer=Integer.parseInt(point);
final ListView courselist = (ListView)findViewById(R.id.courseNames);
LayoutInflater inflater = getLayoutInflater();
ViewGroup header = (ViewGroup)inflater.inflate(R.layout.listviewheader, courselist, false);
courselist.addHeaderView(header, null, false);
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, courses);
courselist.setAdapter(adapter);
courselist.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// TODO Auto-generated method stub
//we use the items of the listview as title of the next activity
if((Integer.parseInt(String.valueOf(point_integer))<=10))
{
}
}
});
}
}
event2icon.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">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/edit_text"
android:text="Enter your points:"/>
<ListView
android:id="@+id/courseNames"
android:layout_width="match_parent"
android:layout_below="@+id/edit_text"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:dividerHeight="15.0sp"
>
</ListView>
</LinearLayout>
listviewheader.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>
您没有找到任何东西,因为您需要分解问题并逐一研究:
你想要一个代表课程的 class,带有名称和分数 字段。
包含这些课程实例的列表(如您所列)。
您需要一个 activity,其中包含供学生输入的文本框 数入.
您需要将文本解析为整数。
您需要迭代您定义的列表。
并且通过使用小于或等于运算符
<=
,您会发现 您需要的课程。在 Android 你应该使用
ListAdapter
很好地显示列表。
你应该能够执行 weston 在他的回答中提到的所有步骤,否则,恐怕我只给你代码不会教你太多。
相反,我会为您提供其他答案中提到的片段。
You want a class that represents the course, with name and points fields
这是一个简单的模型class。 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();
}
}
A list that contains the instances of these courses
你只有字符串,没有在你的代码中列出点数,你怎么会想到用数字来过滤课程呢?这是您作为数组列出的示例课程的示例。
Course[] courses = new Course[]{
new Course("Medicine", 30),
new Course("Physics", 28),
new Course("Math", 24),
new Course("English", 20)
};
You need an activity that includes a text box for student to enter number into
这需要您写一些 XML,您已经在 event2icon.xml
中完成了。我建议您添加一个 Button
,因为在 EditText
中按 Enter 会插入一个换行符。您也可以在创建时只接受数字。这是我建议的完整 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>
You need to parse the text to an integer
你知道怎么做 - 但问题是你什么时候做这个?您发布的代码会在 Activity 加载后立即从 EditText
中获取文本,这将是空的。当您单击按钮时,您应该改为从 EditText
中提取文本。这会让你开始。
Button searchButton = (Button) findViewById(R.id.btn_search);
searchButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
You need to iterate over list you defined. And by using the less than or equal operator <=, you'll find the courses you need
这只是对您定义的 Courses 数组的简单 for-each 循环。找到 Course 对象后,将其添加到适配器中。您还应该知道,您需要将 Course[]
转换为 List<Course>
才能执行此操作,否则会出现错误。
List<Course> courseList = new ArrayList<Course>(Arrays.asList(courses));
// initialize the adapter with courseList instead of courses
final ArrayAdapter<Course> adapter = new ArrayAdapter<Course>(this, android.R.layout.simple_list_item_1, 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);
}
}
Use a ListAdapter to display the list nicely
你已经有一个 ArrayAdapter
,所以你在这里很好。不过,最后一个细节是,当您单击 Button
时,您应该先 clear
适配器,然后再将课程重新添加到其中。否则,你会得到重复的。