Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
214 views
in Technique[技术] by (71.8m points)

java - 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 different points.

E.g:

  • Medicine: 30
  • Physics: 28
  • Maths: 24
  • English: 20

Now suppose a student enter the point 28.

My app should be able to detect the course that has 28 points but also that of lower points. That is; Physics, Maths and English BUT NOT Medicine!!!

Can anyone help me on this? I have searched everywhere but no results yet!

Please guys am having a hard time doing this! Thank you!

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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...