如何根据用户的不同要求仅选择列表视图的一部分?

时间:2016-01-07 15:45:51

标签: android listview select display

我正在尝试开发一个Android移动应用程序,我需要显示至少包含100个项目的列表视图的一部分。 现在,作为一所大学的管理者,我列出了100个要教授的科目。(所有这些都列在列表视图中。所有科目都有一个观点,并且为了让一个学生申请这门课程他/她需要具有更高的分数。一个指示性样本如下: 英语:24 西班牙:16 数学:28 科学:26 法国:16 管理:22 法:30 亚洲语言:10

现在,学生需要输入他/她的观点并以此为基础(确切或较低点),他/她将获得他/她有资格申请的科目列表。

E.g输入你的积分:24

listview中的输出应该提供以下内容: 法国: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>

1 个答案:

答案 0 :(得分:0)

我的解决方案如下 -

  1. MainActivity

    public class MainActivity extends AppCompatActivity {
    
    ListView myList;
    EditText edtSearch;
    Button btnSearch;
    
    listAdapter adapter;
    ArrayList<Course> alSearchCourses;
    Course[] courses;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    
    myList = (ListView) findViewById(R.id.myList);
    edtSearch = (EditText) findViewById(R.id.edtSearch);
    btnSearch = (Button) findViewById(R.id.btnSearch);
    
    courses = new Course[]{
            new Course("Medicine", 30),
            new Course("Physics", 28),
            new Course("Math", 24),
            new Course("English", 20)
    };
    
    adapter = new listAdapter(this, courses);
    myList.setAdapter(adapter);
    
    edtSearch.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }
    
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if (count == 0) {
                adapter = new listAdapter(MainActivity.this, courses);
                myList.setAdapter(adapter);
            }
        }
    
        @Override
        public void afterTextChanged(Editable s) {
        }
    });
    
    btnSearch.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
    
            alSearchCourses = new ArrayList<>();
    
            int points = Integer.parseInt(edtSearch.getText().toString());
            for (int i = 0; i < courses.length; i++) {
    
                Course c2 = courses[i];
    
                if (c2.getPoints() <= points) {
                    alSearchCourses.add(c2);
                }
    
            }
    
            Course searchCourses [] = new Course[alSearchCourses.size()];
            searchCourses = alSearchCourses.toArray(searchCourses);
    
            adapter = new listAdapter(MainActivity.this, searchCourses);
            myList.setAdapter(adapter);
    
        }
    
    });
    
    }
    }
    
  2. listAdapter

    public class listAdapter extends BaseAdapter {
    
    Context context;
    Course[] courses;
    LayoutInflater inflater;
    
    public listAdapter(Context context, Course[] courses) {
    this.context = context;
    this.courses = courses;
    inflater = LayoutInflater.from(context);
    }
    
    @Override
    public int getCount() {
    return courses.length;
    }
    
    @Override
    public Object getItem(int position) {
    return null;
    }
    
    @Override
    public long getItemId(int position) {
    return 0;
    }
    
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
    
    if(convertView == null){
        convertView = inflater.inflate(R.layout.list_item_layout, null);
    }
    
    TextView txtCourse = (TextView) convertView.findViewById(R.id.txtCourse);
    txtCourse.setText(courses[position].getName() + "-" +     courses[position].getPoints());
    
    return convertView;
    }
    }
    
  3. 希望有所帮助:)