Android - 将子项添加到已排序的ListView

时间:2014-07-21 16:31:28

标签: java android listview sorting subitem

我有一个ListView的代码:

private class CollegeItem {
private double gpa;
private int act;
private int sat;
private String name;
private String location;
private double score;
private boolean match;
private double scoreDistance;


private boolean uaero, uagri, ubio, uchem, ucivil, ucomp, uelec, uphys, uenvi, uindus, umate, umech;

public CollegeItem(double gpa, int act, int sat, String name, String location, boolean uaero, boolean uagri, boolean ubio, boolean uchem,
                    boolean ucivil, boolean ucomp, boolean uelec, boolean uphys, boolean uenvi, boolean uindus, boolean umate, boolean umech){
    this.gpa = gpa;
    this.act = act;
    this.sat = sat;
    this.name = name;
    this.location = location;
    this.uaero = uaero;
    this.uagri = uagri;
    this.ubio = ubio;
    this.uchem = uchem;
    this.ucivil = ucivil;
    this.ucomp = ucomp;
    this.uelec = uelec;
    this.uphys = uphys;
    this.uenvi = uenvi;
    this.uindus = uindus;
    this.umate = umate;
    this.umech = umech;

    if(act/36.0>sat/2400.0){
        this.score = 0.6*gpa*25.0+0.4*(act/36.0)*100.0;
    }else{
        this.score = 0.6*gpa*25.0+0.4*(sat/2400.0)*100.0;
    }
    scoreDistance = Math.abs(this.score-MainActivity.scoreDouble)/MainActivity.scoreDouble;
    if(uagri&&ListOfMajors.agricultural||uchem&&ListOfMajors.chem||uaero&&ListOfMajors.aerospace||ubio&&ListOfMajors.biomed||ucivil&&ListOfMajors.civil
            ||ucomp&&ListOfMajors.computer||uelec&&ListOfMajors.electrical||uphys&&ListOfMajors.physics||uenvi&&ListOfMajors.environment||uindus&&ListOfMajors.industrial
            ||umate&&ListOfMajors.materials||umech&&ListOfMajors.mechanical){
        scoreDistance--;
    }else{
        scoreDistance = Math.abs(this.score-MainActivity.scoreDouble)/MainActivity.scoreDouble;
    }
}

ArrayList<CollegeItem> collegeLists=new ArrayList<CollegeItem>();
ArrayList<String> nameList = new ArrayList<String>();

Comparator<CollegeItem> compare = new Comparator<CollegeItem>(){
    public int compare(CollegeItem a, CollegeItem b){
            return Double.compare(a.getScoreDistance(), b.getScoreDistance());
    }
};
 Collections.sort(collegeLists, compare);

    for(CollegeItem collegeList : collegeLists){
        nameList.add(collegeList.getName());
    }

    setListAdapter(new ArrayAdapter<String>(CollegeList.this, android.R.layout.simple_list_item_1, nameList));

截至目前,ListView的顺序按字段变量scoreDistance的升序排列。它通过比较器以这种方式排序。 List的实际内容是字段变量名称。我现在想要将字段变量位置添加为每个名称的子项。它应该仍然以相同的方式排序。我已经查看了有关添加子项的多个问题,但我仍然不确定如何执行此操作。我该怎么办?

0 个答案:

没有答案