我已经解决了如何显示前三名的得分,而不是前三名球员的名字

时间:2018-10-23 14:20:39

标签: android

这是显示三个最高得分的jave文件,但是如果我想显示三个最高得分者的名字,我不知道代码是什么。是否使用三个新的高得分者编写相同的条件结构琴弦?我不想使用Google页首横幅,我想采用与高分相同的结构。

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.content.SharedPreferences;


public class recordofscores extends AppCompatActivity {
    private Button btn_logout;
    private TextView tvEmail, yourscore, highestscore;
    private Session session;

    public static final String MyPREFERENCES="MyPrefs";
    public static final String Name="nameKey";
    SharedPreferences sharedpreferences;
    public static final String savedname = "scorerOne";
    String name;

    int lastScore;
    int best1,best2, best3;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);
        setContentView (R.layout.activity_recordofscores);


        sharedpreferences=getSharedPreferences (MyPREFERENCES, Context.MODE_PRIVATE);



        yourscore=(TextView) findViewById (R.id.yourscore);
        highestscore=(TextView) findViewById (R.id.highestscore);

        tvEmail = (TextView) findViewById (R.id.tvEmail);

        tvEmail.setText (sharedpreferences.getString (Name, "n") + " is logged in");
        SharedPreferences preferences=getSharedPreferences ("PREFS", 0);
        lastScore=preferences.getInt ("lastScore", 0);
        best1=preferences.getInt ("best1", 0);
        best2=preferences.getInt ("best2", 0);
        best3=preferences.getInt ("best3", 0);

        if (lastScore > best3) {
            best3=lastScore;
            SharedPreferences.Editor editor=preferences.edit ( );
            editor.putInt ("best3", best3);
            editor.apply ( );
        }
        if (lastScore > best2) {
            int temp=best2;
            best2=lastScore;
            best3=temp;
            SharedPreferences.Editor editor=preferences.edit ( );
            editor.putInt ("best3", best3);
            editor.putInt ("best2", best2);
            editor.apply ( );
        }
        if (lastScore > best1) {
            int temp=best1;
            best1=lastScore;
            best2=temp;
            SharedPreferences.Editor editor=preferences.edit ( );
            editor.putInt ("best2", best2);
            editor.putInt ("best1", best1);
            editor.apply ( );
        }



        yourscore.setText (sharedpreferences.getString (Name, "n") + " - " + lastScore);
highestscore.setText("1 - " + best1 +" \n" +
                     "2 - " + best2 + "\n" +
                      "3 - " + best3 );





        session = new Session(this);
        if(!session.loggedin()){
            logout();
        }
        btn_logout = (Button)findViewById(R.id.btn_logout);
        btn_logout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                logout();
            }
        });
    }

    private void logout(){
        session.setLoggedin(false);
        finish();
        startActivity(new Intent(recordofscores.this,LoginActivity.class));
    }
}`

1 个答案:

答案 0 :(得分:0)

您可以在String中保存三个sharedpreferences使用Room将数据保存在本地数据库中。 Room将为您提供一种保存表格而不是单个对象的简便方法,因此,如果您想保存更多参数或更多得分(例如最近的100个得分),这将很容易。 Room Guide