如何从4名球员中选出最高分?

时间:2017-07-22 21:48:04

标签: java android

我有一种情况,我想获得一个列表(按钮点击)列出的4个玩家的分数(新活动)(1.名称 - 得分2.名称 - 得分等)从高分到低分。以下是我在Activity中的代码,其中我将姓名和分数发送到活动2。

Button calculate4 = (Button) findViewById(R.id.calculate4);
        calculate4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent myIntent = new Intent(view.getContext(), FourCalculate.class);
                myIntent.putExtra("name1", name1.getText().toString());
                myIntent.putExtra("name2", name2.getText().toString());
                myIntent.putExtra("name3", name3.getText().toString());
                myIntent.putExtra("name4", name4.getText().toString());
                myIntent.putExtra("p1", p1.getText().toString());
                myIntent.putExtra("p2", p2.getText().toString());
                myIntent.putExtra("p3", p3.getText().toString());
                myIntent.putExtra("p4", p4.getText().toString());
                startActivity(myIntent);
            }
        });

这是我在活动2中的代码:

public class FourCalculate extends Activity {

   class Calculate {
    public String Name;
    public int Scor;

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


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_four_calculate);

        // Names
        TextView place1final = (TextView) findViewById(R.id.place1);
        place1final.setText(getIntent().getExtras().getString("name1"));
        String name1final;
        name1final = place1final.getText().toString();

        TextView place2final = (TextView) findViewById(R.id.place2);
        place2final.setText(getIntent().getExtras().getString("name2"));
        String name2final;
        name2final = place2final.getText().toString();

        TextView place3final = (TextView) findViewById(R.id.place3);
        place3final.setText(getIntent().getExtras().getString("name3"));
        String name3final;
        name3final = place3final.getText().toString();

        TextView place4final = (TextView) findViewById(R.id.place4);
        place4final.setText(getIntent().getExtras().getString("name4"));
        String name4final;
        name4final = place4final.getText().toString();

        // Scores
        TextView place1finalscore = (TextView) findViewById(R.id.place1score);
        place1finalscore.setText(getIntent().getExtras().getString("p1"));
        String score1final;
        score1final = place1finalscore.getText().toString();

        TextView place2finalscore = (TextView) findViewById(R.id.place2score);
        place1finalscore.setText(getIntent().getExtras().getString("p2"));
        String score2final;
        score2final = place2finalscore.getText().toString();

        TextView place3finalscore = (TextView) findViewById(R.id.place3score);
        place3finalscore.setText(getIntent().getExtras().getString("p3"));
        String score3final;
        score3final = place3finalscore.getText().toString();

        TextView place4finalscore = (TextView) findViewById(R.id.place4score);
        place4finalscore.setText(getIntent().getExtras().getString("p4"));
        String score4final;
        score4final = place4finalscore.getText().toString();

        int finalscore1;
        finalscore1 = Integer.parseInt(score1final);

        int finalscore2;
        finalscore2 = Integer.parseInt(score2final);

        int finalscore3;
        finalscore3 = Integer.parseInt(score3final);

        int finalscore4;
        finalscore4 = Integer.parseInt(score4final);

        ArrayList<FourCalculate> arrFourCalculate = new ArrayList<>(4);


        //building manually the array list
        arrFourCalculate.add(new Calculate(name1final, finalscore1));
        arrFourCalculate.add(new Calculate(name2final, finalscore2));  
        arrFourCalculate.add(new Calculate(name3final, finalscore3)); 
        arrFourCalculate.add(new Calculate(name4final, finalscore4)); 

        Collections.sort(arrFourCalculate, new Comparator<FourCalculate>() {

            @Override
            public int compare(FourCalculate o1, FourCalculate o2) {
                return Integer.valueOf(o1.Scor).compareTo(o2.Scor);
            }

        });

       place1final.setText(String.valueOf(arrFourCalculate.get(3)));
       place2final.setText(String.valueOf(arrFourCalculate.get(2)));
       place3final.setText(String.valueOf(arrFourCalculate.get(1)));
       place4final.setText(String.valueOf(arrFourCalculate.get(0)));
    }
}

输入姓名和号码后,当我尝试访问活动2时,应用程序崩溃。 logcat的:

07-23 18:32:26.416 12392-12392/com.example.sante.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
                                                                             Process: com.example.sante.myapplication, PID: 12392
                                                                             java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sante.myapplication/com.example.sante.myapplication.FourCalculate}: java.lang.NumberFormatException: For input string: ""
                                                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
                                                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
                                                                                 at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
                                                                                 at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                 at android.os.Looper.loop(Looper.java:154)
                                                                                 at android.app.ActivityThread.main(ActivityThread.java:6119)
                                                                                 at java.lang.reflect.Method.invoke(Native Method)
                                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
                                                                              Caused by: java.lang.NumberFormatException: For input string: ""
                                                                                 at java.lang.Integer.parseInt(Integer.java:533)
                                                                                 at java.lang.Integer.parseInt(Integer.java:556)
                                                                                 at com.example.sante.myapplication.FourCalculate.onCreate(FourCalculate.java:79)
                                                                                 at android.app.Activity.performCreate(Activity.java:6679)
                                                                                 at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
                                                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
                                                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 
                                                                                 at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                                                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) 
                                                                                 at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                 at android.os.Looper.loop(Looper.java:154) 
                                                                                 at android.app.ActivityThread.main(ActivityThread.java:6119) 
                                                                                 at java.lang.reflect.Method.invoke(Native Method) 
                                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 

2 个答案:

答案 0 :(得分:0)

你的错误似乎很明显,这里发生了什么。你 不能 实例化一个活动,所以这个:

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

和此:

arrFourCalculate.add(new FourCalculate(name1final, finalscore1)); 
arrFourCalculate.add(new FourCalculate(name2final, finalscore2)); 
arrFourCalculate.add(new FourCalculate(name3final, finalscore3)); 
arrFourCalculate.add(new FourCalculate(name4final, finalscore4)); 

无法完成。为对象创建单独的模型类

答案 1 :(得分:0)

Finaly!在一些帮助下,我实现了我想要的。分享正确的代码。

public class FourCalculate extends Activity {

    class Calculate {

        public String Name;
        public int Scor;

        public Calculate(String sName, int iScor) {
            this.Name = sName;
            this.Scor = iScor;
        }
        @Override
        public String toString() {
            return Name + " " + Scor;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_four_calculate);

        // Names
        TextView place1final = (TextView) findViewById(R.id.place1);
        place1final.setText(getIntent().getExtras().getString("name1"));
        String name1final;
        name1final = place1final.getText().toString();

        TextView place2final = (TextView) findViewById(R.id.place2);
        place2final.setText(getIntent().getExtras().getString("name2"));
        String name2final;
        name2final = place2final.getText().toString();

        TextView place3final = (TextView) findViewById(R.id.place3);
        place3final.setText(getIntent().getExtras().getString("name3"));
        String name3final;
        name3final = place3final.getText().toString();

        TextView place4final = (TextView) findViewById(R.id.place4);
        place4final.setText(getIntent().getExtras().getString("name4"));
        String name4final;
        name4final = place4final.getText().toString();

        String finalscore1 = getIntent().getExtras().getString("p1");
        String finalscore2 = getIntent().getExtras().getString("p2");
        String finalscore3 = getIntent().getExtras().getString("p3");
        String finalscore4 = getIntent().getExtras().getString("p4");


        finalscore1 = finalscore1.length() > 0 ? finalscore1 : "0";
        finalscore2 = finalscore2.length() > 0 ? finalscore2 : "0";
        finalscore3 = finalscore3.length() > 0 ? finalscore3 : "0";
        finalscore4 = finalscore4.length() > 0 ? finalscore4 : "0";

        ArrayList<Calculate> arrFourCalculate = new ArrayList<>(4);


        arrFourCalculate.add(new Calculate(name1final, Integer.parseInt(finalscore1)));
        arrFourCalculate.add(new Calculate(name2final, Integer.parseInt(finalscore2)));
        arrFourCalculate.add(new Calculate(name3final, Integer.parseInt(finalscore3)));
        arrFourCalculate.add(new Calculate(name4final, Integer.parseInt(finalscore4)));


        Collections.sort(arrFourCalculate, new Comparator<Calculate>() {

            @Override
            public int compare(Calculate o1, Calculate o2) {
                return Integer.valueOf(o1.Scor).compareTo(o2.Scor);
            }

        });

       place1final.setText(String.valueOf(arrFourCalculate.get(3)));
       place2final.setText(String.valueOf(arrFourCalculate.get(2)));
       place3final.setText(String.valueOf(arrFourCalculate.get(1)));
       place4final.setText(String.valueOf(arrFourCalculate.get(0)));
    }
}