简体   繁体   中英

How do I use a SharedPref. in an ArrayAdapter class file?

I have a custom Array Adapter to display a list of items, in this case, highscores. The system sort of mimics a folder scheme; one clicks a folder named "Game 1" and it goes to a new list with the top 10 scorers. When you take a test, it changes a sharedpreferences called isPlayed, and if isPlayed is 1, the image for that Game, Game 1, is green, otherwise, if you never play Game 1, isPlayed is never 0, and therefor the image on the highscore list of games is red. But, SharedPreferences seems to be unknown in a class that extends ArrayAdapter. How can I use data from sharedpreferences here? Post a question if you need more info.

You can access SharedPreferences with -

getContext().getSharedPreferences(String name, int mode)

PreferenceManager.getDefaultSharedPreferences(YourActivity.this)

或者,而不是YourActivity.this您可以使用应用程序中的任何Context ,例如您的应用程序上下文。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM