如何排序对象的ArrayList(玩家名称和玩家得分)

时间:2017-01-27 19:30:28

标签: java android arrays sorting

我尝试解决这个问题,但我不知道自己做错了什么,已经过了7天尝试了很多事情,我需要帮助解决它,我认为错误是在我创建的类。

我有4个玩家,4个文本视图和4个其他文本视图,用于玩家的分数。此外,我为place1,place2,place3,place4创建了另外4个新的文本视图。我想要做的是我需要对得分进行排序并将它们显示在place1,place2,place3,place4以及具有该得分的那个玩家的名字。

我到目前为止所做的是将我的课程创建到onCreate部分上方的主要活动

public class MainActivity extends AppCompatActivity {

    //my created class
    class JucatorScor {

        public String Name;
        public int Scor;

        public JucatorScor (String sName, int iScor) {
            this.Name = sName;
            this.Scor = iScor;
        }
    }

    //begin oncreate
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    //end oncreate
}

我还有一个按钮,当我点击它时,从玩家的文本视图名称,玩家得分和排序后的阵列显示place1,place2,place3,place4这样读取我的值:

//begin button click
public void sorteazaCLICK (View view) {

    //initialization place1, place2, place3, place4 where i will put the sorting value
    TextView varPozitia1 = (TextView) findViewById(R.id.text_loc1);
    TextView varPozitia2 = (TextView) findViewById(R.id.text_loc2);
    TextView varPozitia3 = (TextView) findViewById(R.id.text_loc3);
    TextView varPozitia4 = (TextView) findViewById(R.id.text_loc4);

    //initialization name of players
    TextView varNumeJucator1 = (TextView) findViewById(R.id.text_numeJuc1);
    String sNameJuc1;
    sNameJuc1 = varNumeJucator1.getText().toString();

    TextView varNumeJucator2 = (TextView) findViewById(R.id.text_numeJuc2);
    String sNameJuc2;
    sNameJuc2 = varNumeJucator1.getText().toString();

    TextView varNumeJucator3 = (TextView) findViewById(R.id.text_numeJuc3);
    String sNameJuc3;
    sNameJuc3 = varNumeJucator1.getText().toString();

    TextView varNumeJucator4 = (TextView) findViewById(R.id.text_numeJuc4);
    String sNameJuc4;
    sNameJuc4 = varNumeJucator1.getText().toString();

    //initialization scores
    TextView varScorJucator1 = (TextView) findViewById(R.id.text_scorJuc1);
    String t1;
    t1 = varScorJucator1.getText().toString();

    TextView varScorJucator2 = (TextView) findViewById(R.id.text_scorJuc2);
    String t2;
    t2 = varScorJucator2.getText().toString();

    TextView varScorJucator3 = (TextView) findViewById(R.id.text_scorJuc3);
    String t3;
    t3 = varScorJucator1.getText().toString();

    TextView varScorJucator4 = (TextView) findViewById(R.id.text_scorJuc4);
    String t4;
    t4 = varScorJucator1.getText().toString();


    //transform the values from text view scores into integer
    int iScorJuc1;
    iScorJuc1 = Integer.parseInt(t1);

    int iScorJuc2;
    iScorJuc2 = Integer.parseInt(t2);

    int iScorJuc3;
    iScorJuc3 = Integer.parseInt(t3);

    int iScorJuc4;
    iScorJuc4 = Integer.parseInt(t4);


    ArrayList<JucatorScor> arrJucatorScor = new ArrayList<>(4);


        //building manually the array list
        arrJucatorScor.add(new JucatorScor(sNameJuc1, iScorJuc1));  //position 0
        arrJucatorScor.add(new JucatorScor(sNameJuc2, iScorJuc2));  //position 1
        arrJucatorScor.add(new JucatorScor(sNameJuc3, iScorJuc3));  //position 2
        arrJucatorScor.add(new JucatorScor(sNameJuc4, iScorJuc4));  //position 3

    Collections.sort(arrJucatorScor, new Comparator<JucatorScor>() {

        //sorting my object array depending on scores
        @Override
        public int compare(JucatorScor o1, JucatorScor o2) {
            return Integer.valueOf(o1.Scor).compareTo(o2.Scor);
        }

    });


    //here display them from bigger to smaller according to eatch place1, place2, place3, place4
    varPozitia1.setText(String.valueOf(arrJucatorScor.get(3)));
    varPozitia2.setText(String.valueOf(arrJucatorScor.get(2)));
    varPozitia3.setText(String.valueOf(arrJucatorScor.get(1)));
    varPozitia4.setText(String.valueOf(arrJucatorScor.get(0)));

} //end button click

问题在于,当我点击按钮时,不要放置place1,place2,place3,place4,名称和分数排序......只是出现在这张照片中的一些内容:

screenshot

2 个答案:

答案 0 :(得分:2)

您可以使用toString() JucatorScor方法渲染对象:

varPozitia1.setText(String.valueOf(arrJucatorScor.get(3)));
varPozitia2.setText(String.valueOf(arrJucatorScor.get(2)));
varPozitia3.setText(String.valueOf(arrJucatorScor.get(1)));
varPozitia4.setText(String.valueOf(arrJucatorScor.get(0)));

您在屏幕上看到的内容是基类提供的默认实现toString()

您可以使用希望的文本

实施toString()

或者另一个解决方案是在辅助方法中执行渲染,并在String中设置合适的TextView

public set renderTopJucatorScor(JucatorScor jucatorScor, int ranking, TextView varPozitia){    
    varPozitia.setText("Loc" + ranking +  " = " + jucatorScor.getName() + jucatorScor.getScore());
}

然后你可以像这样调用辅助方法:

renderTopJucatorScor(varPozitia1, arrJucatorScor.get(3), 1);
renderTopJucatorScor(varPozitia2, arrJucatorScor.get(2), 2);
renderTopJucatorScor(varPozitia3, arrJucatorScor.get(1), 3);
renderTopJucatorScor(varPozitia4, arrJucatorScor.get(0), 4);

答案 1 :(得分:1)

在发现(在用户“davidxxx”的帮助下),在我的textview初始化中出现了一些错误,我终于设法解决了这个问题。我还在我创建的类中添加了一个方法:toString。

正确的解决方案是:

//my created class
class JucatorScor {

    public String Name;
    public int Scor;

    public JucatorScor (String sName, int iScor) {
        this.Name = sName;
        this.Scor = iScor;
    }
//this is the toString added to diplay name and score 
    @Override
    public String toString() {
        return Name + " " + Scor;
    }

}

并点击按钮中的exectution代码

//begin button click
public void sorteazaCLICK(View view) {

    //initialization of textvies for place1 to place 4
    TextView varPozitia1 = (TextView) findViewById(R.id.text_loc1);
    TextView varPozitia2 = (TextView) findViewById(R.id.text_loc2);
    TextView varPozitia3 = (TextView) findViewById(R.id.text_loc3);
    TextView varPozitia4 = (TextView) findViewById(R.id.text_loc4);

    //initialization for name of players
    TextView varNumeJucator1 = (TextView) findViewById(R.id.text_numeJuc1);
    String sNameJuc1;
    sNameJuc1 = varNumeJucator1.getText().toString();

    TextView varNumeJucator2 = (TextView) findViewById(R.id.text_numeJuc2);
    String sNameJuc2;
    sNameJuc2 = varNumeJucator2.getText().toString();

    TextView varNumeJucator3 = (TextView) findViewById(R.id.text_numeJuc3);
    String sNameJuc3;
    sNameJuc3 = varNumeJucator3.getText().toString();

    TextView varNumeJucator4 = (TextView) findViewById(R.id.text_numeJuc4);
    String sNameJuc4;
    sNameJuc4 = varNumeJucator4.getText().toString();

    //initialization of scores
    TextView varScorJucator1 = (TextView) findViewById(R.id.text_scorJuc1);
    String t1;
    t1 = varScorJucator1.getText().toString();

    TextView varScorJucator2 = (TextView) findViewById(R.id.text_scorJuc2);
    String t2;
    t2 = varScorJucator2.getText().toString();

    TextView varScorJucator3 = (TextView) findViewById(R.id.text_scorJuc3);
    String t3;
    t3 = varScorJucator3.getText().toString();

    TextView varScorJucator4 = (TextView) findViewById(R.id.text_scorJuc4);
    String t4;
    t4 = varScorJucator4.getText().toString();


    //transformation scores into integer values
    int iScorJuc1;
    iScorJuc1 = Integer.parseInt(t1);

    int iScorJuc2;
    iScorJuc2 = Integer.parseInt(t2);

    int iScorJuc3;
    iScorJuc3 = Integer.parseInt(t3);

    int iScorJuc4;
    iScorJuc4 = Integer.parseInt(t4);


    ArrayList<JucatorScor> arrJucatorScor = new ArrayList<>(4);


    //manually construct of the object array list
    arrJucatorScor.add(new JucatorScor(sNameJuc1, iScorJuc1));  //position 0
    arrJucatorScor.add(new JucatorScor(sNameJuc2, iScorJuc2));  //position 1
    arrJucatorScor.add(new JucatorScor(sNameJuc3, iScorJuc3));  //position 2
    arrJucatorScor.add(new JucatorScor(sNameJuc4, iScorJuc4));  //position 3

    Collections.sort(arrJucatorScor, new Comparator<JucatorScor>() {

        //the sort of the object list by their scores
        @Override
        public int compare(JucatorScor o1, JucatorScor o2) {
            return Integer.valueOf(o1.Scor).compareTo(o2.Scor);
        }

    });


    //here are displayed in the position place1 to place4 from bigger score to smaller score
    varPozitia1.setText(String.valueOf(arrJucatorScor.get(3)));
    varPozitia2.setText(String.valueOf(arrJucatorScor.get(2)));
    varPozitia3.setText(String.valueOf(arrJucatorScor.get(1)));
    varPozitia4.setText(String.valueOf(arrJucatorScor.get(0)));


}//end button click

非常感谢你的帮助。