I'm trying to make a Favorite list.
i make a SharedPreferences class that store true values and then i need to use of all true values.
it's my class :
public class Baham_SharedPreferences
{
private SharedPreferences prefs;
private static final String NAME = "pref";
public SharedPreferences(Context context) {
prefs = context.getSharedPreferences(NAME, Context.MODE_PRIVATE);
}
public boolean getState(String key) {
return prefs.getBoolean(key, false);
}
public void setState(String key, boolean value){
Editor editor = prefs.edit();
editor.putBoolean(key, value);
editor.commit();
}
public void getall()
{
Favorite favorite=new Favorite(); //Favorite is a fragment class
favorite.fav_list=new ArrayList<String>();
Map<String,?> keys = prefs.getAll();
int i=0;
for(Map.Entry<String,?> entry : keys.entrySet())
{
if (entry.getValue().toString()=="true")
{
favorite.fav_list.add(entry.getKey());
Log.d("true value", favorite.fav_list.get(i));
i++;
}
}
}
}
via getall() i try to New public ArrayList<String> fav_list;
that declared in declare Favorite class that extend from Fragment.
all things are good to this step! , but in Favorite fragment class my fav_list
ArrayList are empty yet - i use this way before (But in activity , not fragment) and work rightly but in fragment class not work ;
First , i call preferences.getall() then use ArrayList fav_list but return this errors :
E/AndroidRuntime(10015): FATAL EXCEPTION: main
E/AndroidRuntime(10015): java.lang.NullPointerException
E/AndroidRuntime(10015): at baham.slidingmenu.Favorite.onCreateView(Favorite.java:52)
E/AndroidRuntime(10015): at android.app.Fragment.performCreateView(Fragment.java:1695)
E/AndroidRuntime(10015): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:885)
E/AndroidRuntime(10015): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1057)
E/AndroidRuntime(10015): at android.app.BackStackRecord.run(BackStackRecord.java:682)
E/AndroidRuntime(10015): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1435)
Now! , it's not any way to pass ArrayList From a class like : public class SharedPreferencesn {...}
(that not extend from anything) to a Fragment class
?
(For Example via : bundle.putStringArrayList(key, value) , intent.putExtras and ... )
Edit :
so , i try this link.
Try this way,hope this will help you to solve your problem.
public class Baham_SharedPreferences{
private SharedPreferences prefs;
private static final String NAME = "pref";
public Baham_SharedPreferences(Context context) {
prefs = context.getSharedPreferences(NAME, Context.MODE_PRIVATE);
}
public boolean getState(String key) {
return prefs.getBoolean(key, false);
}
public void setState(String key, boolean value){
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean(key, value);
editor.commit();
}
public ArrayList<String> getall(){
ArrayList<String> fav_list=new ArrayList<String>();
Map<String,?> keys = prefs.getAll();
for(Map.Entry<String,?> entry : keys.entrySet()){
if (entry.getValue().toString().equals("true")){
fav_list.add(entry.getKey());
}
}
return fav_list;
}
}
Fragment
ArrayList<String> favList = new Baham_SharedPreferences(getActivity()).getall();