ListView没有正确订购

时间:2014-07-17 01:13:46

标签: java android listview collections compare

我正在创建一个应用程序,用户输入他们感兴趣的GPA,SAT和ACT分数和专业,应用程序根据他们与用户输入的分数的匹配程度,按顺序列出大学,如果是大学擅长选拔的专业。大学从最好的比赛变为最差。当只输入数字分数时,我让他们正确订购,但现在我想让选定的专业考虑因素。如果用户选择专业并且大学擅长专业(我为每个专业制作了布尔变量)那些大学应该来到不擅长用户选择的专业的大学之前。这意味着在分数之前会查看专业。现在,这些专业人士似乎根本没有考虑因素。

MajorsList

public class ListOfMajors extends Activity {
    public static boolean aerospace, agricultural, biomed, chem, civil, computer, electrical, physics, environment, industrial, materials, mechanical;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.majorslist);
        ListView mylist = (ListView) findViewById(R.id.majorslist);
        String[] list={"Aerospace Engineering","Agricultural Engineering","Biomedical Engineering","Chemical Engineering","Civil Engineering",
                        "Computer Engineering","Electrical Engineering","Engineering Physics","Environmental Engineering","Industrial Engineering",
                        "Materials Engineering","Mechanical Engineering"};
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(ListOfMajors.this,android.R.layout.simple_list_item_multiple_choice,list);
        mylist.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        mylist.setAdapter(adapter);

        SparseBooleanArray checkedItems = mylist.getCheckedItemPositions();
        if (checkedItems!= null){
            for(int i=0; i<checkedItems.size();i++){
                if(checkedItems.valueAt(0)){
                    aerospace = true;
                }
                if(checkedItems.valueAt(1)){
                    agricultural = true;
                }
                if(checkedItems.valueAt(2)){
                    biomed = true;
                }
                if(checkedItems.valueAt(3)){
                    chem = true;
                }
                if(checkedItems.valueAt(4)){
                    civil = true;
                }
                if(checkedItems.valueAt(5)){
                    computer = true;
                }
                if(checkedItems.valueAt(6)){
                    electrical = true;
                }
                if(checkedItems.valueAt(7)){
                    physics = true;
                }
                if(checkedItems.valueAt(8)){
                    environment = true;
                }
                if(checkedItems.valueAt(9)){
                    industrial = true;
                }
                if(checkedItems.valueAt(10)){
                    materials = true;
                }
                if(checkedItems.valueAt(11)){
                    mechanical = true;
                }
            }
        }

    }


}

CollegeList

private class CollegeItem {
private double gpa;
private int act;
private int sat;
private String name;
private String location;
private double score;
private double scoreDistance;
private double majorsDistance;
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;
    }
    this.scoreDistance = Math.abs(this.score-MainActivity.scoreDouble)/MainActivity.scoreDouble;
    if(uaero&&ListOfMajors.aerospace){
        majorsDistance = 0;
    }
}
public String getName(){
    return this.name;
}
public double getScoreDistance(){
    return this.scoreDistance;
}
public double getMajorsDistance(){
    return this.majorsDistance;
}
public class CollegeList extends ListActivity {

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){
        int result = Double.compare(a.getMajorsDistance(), b.getMajorsDistance());
        if(result==0){
            result = Double.compare(a.getScoreDistance(), b.getScoreDistance());
        }
        return result;
    }
};


CollegeItem michigan = new CollegeItem(3.79,30,2020,"University of Michigan","Ann Arbor, Michigan",true,false,false,false,false,false,false,false,false,false,false,false);
CollegeItem berkeley = new CollegeItem(3.84,30,2040,"University of California Berkeley","Berkeley, California",false,false,false,false,false,false,false,false,false,false,false,false);
CollegeItem stanford = new CollegeItem(3.96,33,2215,"Stanford University","Stanford, California",false,false,false,false,false,false,false,false,false,false,false,false);

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    collegeLists.add(michigan);
    collegeLists.add(berkeley);
    collegeLists.add(stanford); 

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



}

当我检查第一个专业(航空航天)时,无论我输入的数字是多少,我都希望密歇根州成为该名单中的第一所大学,因为它具有该专业的真正价值。但截至目前,它并不是第一位的。我相信要么我要比较错误的对象,要么选择未被注册的专业。无论哪种方式,我该怎么做才能使我的应用程序正常工作?

1 个答案:

答案 0 :(得分:0)

将元素添加到列表中提及元素的位置。只有这样才能按正确顺序排列列表。

格式: list.add(位置,项目);


实施例。 collegeLists.add(1,密歇根州);