You should be able to perform all the steps weston mentioned in his answer, otherwise, I’m afraid me just giving you the code won’t teach you very much.
Instead, I will provide you the pieces mentioned in the other answer.
You want a class that represents the course, with name and points fields
This is a simple model class. An introduction to Java course teaches these.
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
You just have Strings, not points listed in your code, so how did you expect to use numbers to filter the courses? Here is an example of the sample courses you listed as an array.
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
This requires you to write some XML, which you have already done in event2icon.xml
. I suggest you add a Button
, though because hitting Enter within the EditText
this will insert a newline. You could also accept only numbers while you are creating this. Here is the full XML I suggest.
<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
You know how to do this – the question, though is when do you do this? The code you posted immediately gets the text out of the EditText
as soon as the Activity is loaded, which will be empty. You should instead extract the text from the EditText
when you click the button. This will get you started.
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
This is just a simple for-each loop over the Courses array you have defined. Once you have found the Course object, add it to the adapter. You should also be aware that you’ll need to convert the Course[]
to a List<Course>
in order to do this, otherwise you’ll get an error.
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
You already have an ArrayAdapter
, so you are good here. One last detail, though, is that when you click the Button
, you should first clear
the adapter before adding courses back into it. Otherwise, you’ll get duplicates.
5
solved How to filter a List of objects in Android