SharedPreference中的名称和时间?

时间:2014-09-09 04:24:42

标签: android database eclipse

我是Android eclipse编程的新手,我想添加一个数据库,它将保存播放器的输入名称以及完成游戏并在高分模块上显示所花费的时间。可能吗?任何人都可以指导我这个吗?感谢您的任何建议,请对我们好。非常感谢。

更新当游戏结束时,会出现一个警告对话框,要求输入名称。 继承我的代码。

    private Dialog createHighScoreNameDialog() {
    final Dialog dialog = new Dialog(this);
    dialog.setContentView(R.layout.high_score_name_dialog);
    dialog.setTitle(R.string.app_name);
    Button okButton = (Button) dialog.findViewById(R.id.high_score_name_ok_button);
    final SharedPreferences preferences =
        PreferenceManager.getDefaultSharedPreferences(getBaseContext());
    String oldPlayersName = preferences.getString("players_name", "");

    EditText playersNameEditText = (EditText) dialog.findViewById(R.id.players_name);
    playersNameEditText.setText(oldPlayersName);

    okButton.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                    dialog.dismiss();
                    String playerName = ((EditText) dialog.findViewById(R.id.players_name))
                                            .getText().toString();
                }
            });
    return dialog;

之后我该怎么做才能将playerName保存在数据库中?

1 个答案:

答案 0 :(得分:0)

您可以使用下面给出的代码保存任意数量的分数并检索它们。创建一个名为DatabaseHandler.java的新数据库帮助程序类,并在其中添加以下代码。

要初始化您的活动中的课程,请在您的活动中添加以下内容:

DatabaseHandler db = new DatabaseHandler(this);

然后为db use添加值db.addScore(count);

要从db中过滤掉前十个分数,您可以将get方法中的查询更改为:

String selectQuery = "SELECT  * FROM " + TABLE_SCORE + "LIMIT 10";

public class DatabaseHandler extends SQLiteOpenHelper {

    // All Static variables
    // Database Version
    private static final int DATABASE_VERSION = 1;

    // Database Name
    private static final String DATABASE_NAME = "game";

    // Table name
    private static final String TABLE_SCORE = "score";

    // Score Table Columns names
    private static final String KEY_ID_SCORE = "_id";
    private static final String KEY_SCORE = "score_value";

    public DatabaseHandler(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    // Creating Tables
    @Override
    public void onCreate(SQLiteDatabase db) {
        String CREATE_SCORE_TABLE = "CREATE TABLE " + TABLE_SCORE + "("
                + KEY_ID_SCORE + " INTEGER PRIMARY KEY AUTOINCREMENT,"
                + KEY_SCORE + " TEXT" + ")";

        db.execSQL(CREATE_SCORE_TABLE);

    }

    // Upgrading database
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // Drop older table if existed
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_SCORE);

        // Create tables again
        onCreate(db);
    }

    // Adding new score
    public void addScore(int score) {

        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();

        values.put(KEY_SCORE, score); // score value

        // Inserting Values
        db.insert(TABLE_SCORE, null, values);

        db.close();

    }

    // Getting All Scores
    public String[] getAllScores() {

        // Select All Query
        String selectQuery = "SELECT  * FROM " + TABLE_SCORE;

        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

        // looping through all rows and adding to list

        int i = 0;

        String[] data = new String[cursor.getCount()];

        while (cursor.moveToNext()) {

            data[i] = cursor.getString(1);

            i = i++;

        }
        cursor.close();
        db.close();
        // return score array
        return data;
    }}

在表格分数中再添加一个字段名称,然后您可以通过在添加分数时将其作为参数传递来保存名称以及分数。