2011-12-05 9 views
1

私はEclipse上に構築されたアンドロイドアプリを持っています。私が現在やっているのは、xmlファイルの質問をランダムに聞いてみることです。 XMLファイルは次のようになります。アンドロイドアプリで変数の状態を保存して使用する方法

<item> 
    <ques></ques> 
    <option1></option1> 
    <option2></option2> 
    <option3></option3> 
    <ans></ans> 
    <level>1</level> 
</item> 
<item> 
    <ques></ques> 
    <option1></option1> 
    <option2></option2> 
    <option3></option3> 
    <ans></ans> 
    <level>1</level> 
</item> 
<item> 
    <ques></ques> 
    <option1></option1> 
    <option2></option2> 
    <option3></option3> 
    <ans></ans> 
    <level>2</level> 
</item> 

ように....

今私は、ランダムにレベル1からの質問を選択しています。レベル1には50の質問、レベル2には50の質問があります。今すぐ質問を選択したいと思います。上から下に向かって始まります。ユーザーAがログインした場合と同様に、彼はレベル1の質問aとbに質問されているゲームをプレイします。その後、彼はゲームを終了し、再度ログを取ってcとdを表示します。

私の問題は、どのようにこの状態をアンドロイドに保存できるのですか?これを行う簡単な方法はありますか? Androidデバイス上の

答えて

6

何をあなたができることは、各レベルのためですが、それはユーザーがオンになっている問題を追跡するレベル1質問int型の変数について

の番号が付けられています。ユーザーは今、プレイヤーはアプリのうち、活動やログを離れると

questionNumber++; 

になる各質問のために宣言するあなたの方法で

int questionNumber; 

を持つことができます。

このようShared Preferenceに質問番号を入れ..

SharedPreferences app_preferences = 
     PreferenceManager.getDefaultSharedPreferences(this); 

SharedPreferences.Editor editor = app_preferences.edit(); 
    editor.putInt("questionNumber", questionNumber); 
    editor.commit(); // Very important 

を今だけ使用数を引き出すために...

SharedPreferences app_preferences = 
     PreferenceManager.getDefaultSharedPreferences(this); 

    // Get the value for the run counter 
    questionNumber = app_preferences.getInt("questionNumber", 0);// The 0 is there for if the user hastn played before it is set to 0 automatically or you can set it to 1 

EDIT:

また、あなたが持っている可能性をユーザーがいるレベルを追跡する変数

int userLevel; 

これまでと同じように共有設定に保存してください。

1

最も簡単な方法は、共有設定を使用することです。関連するドキュメントの参照先については、ガイドトピックData Storageを参照してください。

関連する問題