Skip to content Skip to sidebar Skip to footer

How To Filter A List Of Objects In Android

I am trying to create an android application (New to it)! My question is as follows: Suppose I am an administrator of a university X. I need to put a list of courses each with dif

Solution 1:

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.

publicclassCourse {

    privateString name;
    private int points;

    publicCourse(String name, int points) {
        this.name = name;
        this.points = points;
    }

    publicStringgetName() {
        return name;
    }

    public int getPoints() {
        return points;
    }

    @OverridepublicStringtoString() {
        returngetName();
    }
}

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[]{
        newCourse("Medicine", 30),
        newCourse("Physics", 28),
        newCourse("Math", 24),
        newCourse("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.

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><EditTextandroid: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"/><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Search"android:id="@+id/btn_search"/></LinearLayout><ListViewandroid: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.

ButtonsearchButton= (Button) findViewById(R.id.btn_search);
searchButton.setOnClickListener(newView.OnClickListener() {
    @OverridepublicvoidonClick(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 = newArrayList<Course>(Arrays.asList(courses));
// initialize the adapter with courseList instead of courses final ArrayAdapter<Course> adapter = newArrayAdapter<Course>(this, android.R.layout.simple_list_item_1, courseList);

// this code inside the Button's onClickint 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.

Solution 2:

You didn't find anything because you need to break the problem up and research each little bit:

  • You want a class that represents the course, with name and points fields.

  • A list that contains the instances of these courses (as you listed).

  • You need an activity that includes a text box for student to enter number into.

  • You need to parse the text to an integer.

  • You need to iterate over list you defined.

  • And by using the less than or equal operator <=, you'll find the courses you need.

  • In Android you should use a ListAdapter to display the list nicely.

Post a Comment for "How To Filter A List Of Objects In Android"