I am developing a dictionary app. I have the following code which show 20 most recent history word.
public List<Bean> getHistoryWords() {
SQLiteDatabase db = initializer.getReadableDatabase();
String sql = "SELECT * FROM " + HISTORY_NAME +
" WHERE " + STATUS + " ORDER BY " + STATUS + " DESC LIMIT 20" ;
Cursor cursor = db.rawQuery(sql, null);
List<Bean> wordList = new ArrayList<Bean>();
while(cursor.moveToNext()) {
int id = cursor.getInt(0);
String english = cursor.getString(1);
String bangla = cursor.getString(2);
String status = cursor.getString(3);
wordList.add(new Bean(id, english, bangla, status));
}
cursor.close();
db.close();
return wordList;
}
I would like to provide an option for user to change the number of history item from LIMIT 20
to LIMIT 50
or LIMIT 100
through Preferences
.
I follow this tutorial, but I was stuck in "5) Saving/reading data". I tried to put the SharedPreferences
under getHistoryWords
, but I got error cannot resolve getBaseContext
.
public List<Bean> getHistoryWords() {
SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
Where should I put the code?
There is a simplest way for it.
First, create YourAppName , that extends Application
Declare Context
as static and init it on Application 's onCreate method,
public YourApp extends Application {
private static Context context;
public void onCreate() {
super.onCreate();
YourApp.context = getApplicationContext();
}
public static Context getAppContext() {
return YourApp.context;
}
}
declare YourApp in Manifest.xml
android:name="com.yourpackagename.app.YourApp"
in application tag,
Now, you can get for Context, every where you need like this,
YourApp.getAppContext()
I hope this will help for you.