2011-12-05 4 views
1

Eclipse에 구축 된 Android 앱이 있습니다. 내가 현재하고있는 일은 xml 파일의 질문을 무작위로 묻는 것이다. XML 파일은 다음과 같습니다android app에서 변수의 상태를 저장하고 사용하는 방법

<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를보아야합니다.

내 문제는 어떻게이 상태를 안드로이드에 저장할 수 있습니까? 이 작업을 수행하는 쉬운 방법이 있습니까? 안드로이드 장치에

답변

6

, 그것은 사용자가 켜져 질문을 추적하기 위해 레벨 1 문제에 대해서는 INT 변수를

으로 번호가 각 수준입니다. 플레이어는 응용 프로그램에서 활동 또는 로그를 남긴다 이제 때 당신은 당신의 방법에

int questionNumber; 

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 

편집 :

은 또한 당신이있을 수 있습니다 사용자가있는 수준을 추적하는 변수

int userLevel; 

그리고 전에했던 것처럼 공유 환경 설정에 저장하십시오.

1

가장 쉬운 방법은 공유 환경 설정을 사용하는 것입니다. 관련 설명서에 대한 지침은 가이드 항목 Data Storage을 참조하십시오. 당신이 할 수있는 것은

관련 문제