2014-02-14 37 views
0

我正在開發一個應用程序,在其中實現下一個和上一個功能。用戶可以通過此功能前進或後退故事。我在共享偏好設置中存儲了故事的當前名稱,但現在我被卡住了。我不知道如何讀取string.xml中定義的故事的名稱。那麼我應該怎麼做才能實現下一個/上一個功能。android中的下一個按鈕功能

SharedPreferences currentstory = getSharedPreferences("myprefs",Context.MODE_PRIVATE); 
String currstory= currentstory.getString("currentstory","mystory"); 

sharedpreference我已存儲的電流故事的名字。因此,我將通過getsharedpreferences()在其他活動中獲得當前故事的名稱。但我想要獲得story.xml中定義的前一個故事的storyname

+0

請一定要清楚你要問什麼。如何瀏覽故事或如何從XML讀取它們?你有沒有累嗎? – Tariq

回答

0

您可以從string.xml讀取字符串,期運用此代碼:

Resources resources = getResources(); 
final String storyName = resources.getString(R.string.story_name); 
SharedPreferences currentstory = getSharedPreferences("myprefs",Context.MODE_PRIVATE); 
String currstory= currentstory.getString("currentstory", storyName); 

並保存:

SharedPreferences currentstory = getSharedPreferences("myprefs",Context.MODE_PRIVATE); 
Editor editor = currentstory.editor(); 
editor.putString("currentstory", "New Story Name"); 
editor.commit(); 
editor = null; 
+0

在sharedpreference中我已經存儲了當前故事的名稱。因此,我將通過在onother活動中的getsharedpreferences()獲取當前故事的名稱..但是我想要獲取在stroy.xml中定義的前一個故事的故事名稱。 – user3278308

+0

使用string.xml:'故事1的名字',並且在activity:'resources.getString(R.string.story_1);'或者你可以定義'string-array',並且存儲此數組中的故事的sharedPref索引。然後'String [] stories = resources.getStringArray(R.array.stories);'和currentStory是'stories [currentIndex]'next和previous:'stories [currentIndex - 1];故事[currentIndex + 1];' – Drafff