【问题标题】:How do I store game scores including team names for a basketball score counter app?如何为篮球计分器应用程序存储包括球队名称在内的比赛分数?
【发布时间】:2016-08-28 15:20:32
【问题描述】:

我正在尝试存储比赛比分,包括球队名称和在我的篮球比赛计数器应用程序中单击保存按钮时存储的日期。

这是Java代码:

package com.example.android.courtcounter;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

int scoreTeamA = 0;
int scoreTeamB = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        displayForTeamA(0);
        displayForTeamB(0);
        setTitle("Basketball Count");
        Toast.makeText(getApplicationContext(), "You can change team names and scores manually by clicking on them", Toast.LENGTH_LONG).show();
    }

    // +3 Points for Team A
    public void addThreeForTeamA (View view) {
        scoreTeamA = scoreTeamA + 3;
        displayForTeamA(scoreTeamA);
    }

    // +2 Points for Team A
    public void addTwoForTeamA (View view) {
        scoreTeamA = scoreTeamA + 2;
        displayForTeamA(scoreTeamA);
    }

    // +1 Point for Team A
    public void addOneForTeamA (View view) {
        scoreTeamA = scoreTeamA +1;
        displayForTeamA(scoreTeamA);
    }

    // Displays the given score for Team A.

    public void displayForTeamA(int score) {
        TextView scoreView = (TextView) findViewById(R.id.team_a_score);
        scoreView.setText(String.valueOf(score));
    }

    // +3 Points for Team B
    public void addThreeForTeamB (View view) {
        scoreTeamB = scoreTeamB + 3;
        displayForTeamB(scoreTeamB);
    }

    // +2 Points for Team B
    public void addTwoForTeamB (View view) {
        scoreTeamB = scoreTeamB + 2;
        displayForTeamB(scoreTeamB);
    }

    // +1 Point for Team B
    public void addOneForTeamB (View view) {
        scoreTeamB = scoreTeamB +1;
        displayForTeamB(scoreTeamB);
    }

    // Displays the given score for Team B.

    public void displayForTeamB(int score) {
        TextView scoreView = (TextView) findViewById(R.id.team_b_score);
        scoreView.setText(String.valueOf(score));
    }

    public void resetScore (View view) {
        scoreTeamA = 0;
        scoreTeamB = 0;
        displayForTeamA(scoreTeamA);
        displayForTeamB(scoreTeamB);
    }
}

我已将分数和团队名称存储在 EditText 视图中,并希望保存它们并稍后加载它们。有人知道使用什么来保存和加载特定的游戏分数吗?

【问题讨论】:

  • 使用SQLite database 存储分数。
  • @SanketMakani 为此目的,数据库将是过度杀伤力。 SharedPreferences 更容易使用,并且很好地涵盖了 OP 的用例。
  • @OP 您需要指定您尝试收集的数据的深度。没有那个,很难说你要做什么,请看我上面的评论,如果这与你想做的一致,请更新你的帖子。在尝试收集数据之前了解您的数据及其用途。并且返回一个团队的得分总和就像在数据库中查询一个团队的所有得分(在一定范围内)然后返回该值一样简单。

标签: java android


【解决方案1】:

我从 Udacity 出色的“Android 入门”课程中认出了这段代码。您必须使用 sharedPreferences 来保存分数。例如:

SharedPreferences sharedPref = getSharedPreferences(
                getString(R.string.preference_file_key), Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt("scoreTeamA", scoreTeamA);
editor.putInt("scoreTeamB", scoreTeamB);
editor.apply();

然后检索值:

SharedPreferences sharedPref = getSharedPreferences(
                getString(R.string.preference_file_key), Context.MODE_PRIVATE);
int scoreTeamA = sharedPref.getInt(scoreTeamA, 0);
int scoreTeamB = sharedPref.getInt(scoreTeamB, 0);

【讨论】:

  • 这就是你所需要的,sharedPreferences 在应用程序运行之间不会消失。只有在您卸载应用后它们才会消失。
  • 非常感谢,我试试这样实现
【解决方案2】:

您可以使用数据库,但我认为这超出了您的需要。 你也可以使用文件:

  1. 在你的 manifest.xml 中添加这些:

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    
  2. 添加两个函数:

    void writeToFile(String string) {
        FileOutputStream outputStream = openFileOutput("thiswillbethenameofyourfile", Context.MODE_PRIVATE);
        outputStream.write(string.getBytes());
        outputStream.close();
    } 
    
    String readFromFile() {
        String content;
        FileInputStream inputStream = openFileInput("thiswillbethenameofyourfile");
        byte[] buffer = new byte[1024];
        int n;
        while ((n = inputStream.read(buffer)) != -1)
        {
             content.append(new String(buffer, 0, n));
        }
        return content;
    } 
    
  3. 使用包含分数的字符串调用 WriteToFile。

  4. 调用 readFromFile 以获取该分数。
  5. 如果您应该将所有内容存储在一个文件中,您可以使用多个文件来存储多个数据,但这是一个好的开始!

正如@Andrew Sun 提到的,你可以使用 sharedPreferences

https://developer.android.com/training/basics/data-storage/shared-preferences.html

【讨论】:

    【解决方案3】:

    如果你想为全世界的所有用户创建一个数据库,你应该使用 SQL 或者 php 编程语言。 SQL 用于数据库编辑,php 用于控制数据库编辑。

    但是,如果您只想存储正在使用该应用程序的设备的数据,那么您可以简单地在 android 项目文件中创建一个文本文件。然后你就可以使用代码按照你想要的方式填充文本文件了。

    【讨论】:

      猜你喜欢
      • 2013-08-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多