0

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

现在学生需要输入他/她的分数并基于(精确或低分),他/她得到他/她有资格申请的科目列表。

例如输入您的积分:24

列表视图中的输出应给出以下内容:法语: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>
4

1 回答 1

0

我的解决方案如下 -

  1. 主要活动

    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. 列表适配器

    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;
    }
    }
    

希望能帮助到你 :)

于 2016-01-11T06:50:16.937 回答