简体   繁体   English

具有SharedPreferences的离线高分-Android(java)

[英]Offline highscores with SharedPreferences - Android (java)

I think I have a fully functional trivia game right now. 我想我现在有一个功能齐全的琐事游戏。 At the end of the game on the results page it shows a score(long) and percentage(int). 在游戏结束时,在结果页上它显示得分(长)和百分比(整数)。 I want to implement an offline SharePreferences highscores now where 3 variables are inputted -- rank(int), score(long) and percentage(int). 我现在想实现一个离线SharePreferences高分,在其中输入3个变量-rank(int),score(long)和percent(int)。 I was trying to first just implement a highscore with one of the variables and then add the other 2 later but I seem to be failing badly. 我试图首先使用一个变量实现高分,然后再添加另外两个变量,但是我似乎失败很严重。 You can see in my activity below a lot of commented out code where I was trying stuff and a lot of other attempts have already been deleted. 您可以在下面的活动中看到很多注释掉的代码,这些代码是我尝试的地方,许多其他尝试已被删除。

This is my very first time trying to use SharedPreferences so I don't have a solid grasp on it yet. 这是我第一次尝试使用SharedPreferences,因此我对此还没有足够的了解。

Results.java 结果.java

public class Results extends Activity {

    public static final String SP_NAME = "TEST";
    public static final String INT_PERCENTAGE = "SP_percentage";
    public static final String INT_RANK = "SP_rank";
    public static final String LONG_SCORE = "SP_score";
    private SharedPreferences mPrefs;

    QuestionView qv = new QuestionView();
    ArrayList<Question> queryList = qv.getQueries();

    int cAnswers, wAnswers, percentage, rank;

    long score;

    ArrayList<Question> qs;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.resultsmain);

        mPrefs = getSharedPreferences(SP_NAME, 0);

        cAnswers = getIntent().getIntExtra("correctAnswers", -1);
        wAnswers = getIntent().getIntExtra("wrongAnswers", -1);
        score = getIntent().getLongExtra("score", -1);

        qs = getIntent().getParcelableArrayListExtra("queries");

        Button mainmenuBtn = (Button)findViewById(R.id.mainmenuBtn);
        mainmenuBtn.setText("Main Menu");

        mainmenuBtn.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {
                restart();
            }
        }); 

        //Retrieving high score
        SharedPreferences mPrefs = getSharedPreferences(LONG_SCORE, 0);
        SharePreferences.Editor editor = prefs.edit();
        long highscore = mPrefs.getLong(LONG_SCORE, 0);

        //Saving current score as high score
/*      SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
        SharedPreferences.Editor editor = settings.edit();
        editor.putInteger(HIGH_SCORE, currentScore);*/
        // Commit the edits!
        editor.commit();

        SharedPreferences prefs = mContext.getSharedPreferences("preferencename", 0);  
/*      SharedPreferences.Editor editor = prefs.edit();  
        return editor.commit();*/

        showResults();
    }

    //...other code
}

The point is, you didn't understand the usage of a SharedPreference. 关键是,您不了解SharedPreference的用法。 To this high score stuff, you should use a SQLite database or a text file. 对于这些高分的东西,您应该使用SQLite数据库或文本文件。

Please, read the guide http://developer.android.com/guide/topics/data/data-storage.html to a better understanding of different types of persistence methods in Android. 请阅读指南http://developer.android.com/guide/topics/data/data-storage.html ,以更好地了解Android中不同类型的持久化方法。

BTW, you should create a table with the three columns you said (rank(int), score(long) and percentage(int)) and do a little CRUD to have your high score working well. 顺便说一句,您应该使用所说的三列(rank(整数),score(长整数)和percent(int))创建一个表,并做一点CRUD以使您的高分表现良好。 You will have to understand how to use the SQLiteOpenHelper and create your own, to do the table creations procedures. 您将必须了解如何使用SQLiteOpenHelper并创建自己的表创建过程。 And then, create you model class to manage the data. 然后,创建您的模型类来管理数据。

So, search in Google for SQLite tutorials if you still having doubts after reading the developer.android.com guide. 因此,如果您在阅读developer.android.com指南后仍然有疑问,请在Google中搜索SQLite教程。

It looks like your question got pretty well answered, but I'm just gonna throw this out there for perspective (the more you know! right?). 看来您的问题已得到很好的回答,但我只是将其扔在那里进行观察(您知道的更多!对吗?)。

You may find it easier to use an online leaderboard system instead of dealing with maintaining one locally. 您可能会发现,使用在线排行榜系统比处理本地维护更容易。 It can save you a good bit of work and give you the benefit that players can then easily compete globally. 它可以节省您很多工作,并为您带来好处,让玩家可以轻松地在全球范围内竞争。 Some options to check into are Swarm (aka SwarmConnect), Scoreloop or Papaya . Swarm (aka SwarmConnect), ScoreloopPapaya是要检查的一些选项。 All 3 of them offer a way to get global leaderboards up and running, although I think Swarm's system is probably the fastest and easiest to setup out of the box. 尽管我认为Swarm的系统可能是开箱即用最快,最容易的系统,但所有这三个都提供了一种建立和运行全球排行榜的方法。

Cheers! 干杯!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM