Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/384.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java ListView排序不正确_Java_Android_Listview_Collections_Compare - Fatal编程技术网

Java ListView排序不正确

Java ListView排序不正确,java,android,listview,collections,compare,Java,Android,Listview,Collections,Compare,我正在创建一个应用程序,用户在其中输入他们的GPA、SAT和ACT分数以及他们感兴趣的专业,该应用程序根据他们与用户输入的分数的匹配程度,以及该学院是否擅长所选专业,按顺序列出学院。大学从最好的比赛到最差的比赛。当只输入数字分数时,我让他们正确排序,但现在我想让选定的专业考虑在内。如果用户选择了一个专业,并且学院擅长该专业(我为每个专业设置了布尔变量),那么这些学院应该排在不擅长用户选择的专业的学院之前。这意味着在评分之前先看专业。现在,各专业似乎根本没有考虑到这一点 市长 public cla

我正在创建一个应用程序,用户在其中输入他们的GPA、SAT和ACT分数以及他们感兴趣的专业,该应用程序根据他们与用户输入的分数的匹配程度,以及该学院是否擅长所选专业,按顺序列出学院。大学从最好的比赛到最差的比赛。当只输入数字分数时,我让他们正确排序,但现在我想让选定的专业考虑在内。如果用户选择了一个专业,并且学院擅长该专业(我为每个专业设置了布尔变量),那么这些学院应该排在不擅长用户选择的专业的学院之前。这意味着在评分之前先看专业。现在,各专业似乎根本没有考虑到这一点

市长

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

    }


}
公共类ListOfMajors扩展活动{
公共航空航天、农业、生物医学、化学、民用、计算机、电气、物理、环境、工业、材料、机械;
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.MajorList);
ListView mylist=(ListView)findViewById(R.id.MajorList);
字符串[]列表={“航空航天工程”、“农业工程”、“生物医学工程”、“化学工程”、“土木工程”,
“计算机工程”、“电气工程”、“工程物理”、“环境工程”、“工业工程”,
“材料工程”、“机械工程”};
ArrayAdapter=新的ArrayAdapter(ListOfMajors.this,android.R.layout.simple\u list\u item\u多项选择,list);
mylist.setChoiceMode(ListView.CHOICE\u MODE\u MULTIPLE);
mylist.setAdapter(适配器);
SparseBooleanArray checkedItems=mylist.getCheckedItemPositions();
if(checkedItems!=null){
用于(int i=0;isat/2400.0){
这个分数=0.6*gpa*25.0+0.4*(act/36.0)*100.0;
}否则{
这个分数=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){
主距离=0;
}
}
公共字符串getName(){
返回此.name;
}
公共双getScoreDistance(){
返回此值。计算距离;
}
公共双GetMajorDistance(){
返回此.majorsDistance;
}
公开课的大学生活动{
ArrayList collegeLists=新的ArrayList();
ArrayList nameList=新的ArrayList();
比较器比较=新比较器(){
公共整数比较(集合项a、集合项b){
int result=Double.compare(a.getMajorDistance(),b.getMajorDistance());
如果(结果==0){
结果=Double.compare(a.getScoreDistance(),b.getScoreDistance());
}
返回结果;
}
};
CollegeItem michigan=新的CollegeItem(3.79,302020,“密歇根大学”,“密歇根州安娜堡”,真,假,假,假,假,假,假,假,假,假);
伯克利学院=新学院项目(3.84,302040,“加州大学伯克利分校”,“加州伯克利”,假,假,假,假,假,假,假,假,假,假,假,假,假,假,假);
CollegeItem stanford=新的CollegeItem(3.96,332215,“斯坦福大学”,“加利福尼亚州斯坦福”,假,假,假,假,假,假,假,假,假,假,假,假,假,假);
@凌驾
创建时受保护的void(Bundle savedInstanceState){
//TODO自动生成的方法存根
super.onCreate(savedInstanceState);
加(密歇根州);
加上(伯克利);
加(斯坦福);
收集。排序(收集者,比较);
用于(学院项目学院讲师:学院讲师){
add(colleclist.getName());
}
setListAdapter(新的ArrayAdapter(colleclist.this,android.R.layout.simple_list_item_1,nameList));
}

当我检查第一专业(航空航天)时,无论我输入的数字是多少,我都希望密歇根大学成为列表中的第一所大学,因为它对该专业有着真正的价值。但到目前为止,它还没有排在第一位。我认为要么我比较的对象错误,要么所选专业没有注册。不管怎样,我应该怎么做才能让我的申请生效?

在向列表添加元素时,请提及元素的位置。只有这样才能按正确的顺序排列列表

格式:列表。添加(位置、项目);



示例。collegeLists.add(密歇根州1号);

是的。如果要按正确的顺序列出,则必须为元素添加位置。@感谢您的响应,但顺序或元素将根据输入的值和选中的框而改变。我想我无法使用此方法来完成此操作。您的onClick方法或onChecked方法在哪里他在运行时列出元素吗?@Ash它不在onClick方法上,它是按照CollegeList类中的comparator排序的,因为逻辑comparator是从onCreate()方法调用的,这意味着它将仅在第一次加载活动时设置list。这是您期望的正确条件还是需要自动重新加载相同的列表?
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));



}