Reputation: 2321
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.
Upvotes: 0
Views: 1634
Reputation: 492
Bundles can accept custom classes, if they implement either Parcelable or Serializable..
for example
Bundle bundle = new Bundle();
bundle.putSerializable("MyData", data);
fragment_one.setArguments(bundle);
Now fragment_one will have access to data in it's onCreate(Bundle bundleHoldingData) method.
Upvotes: 1
Reputation: 24848
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();
Upvotes: 1
Reputation: 15775
Your getall()
method is creating a new Favorite
by calling new
. If Favorite
is really a Fragment
then it will not have gone through its onCreate
yet so it will not be initialized.
Upvotes: 1
Reputation: 5348
There are some part of your code i will not understand. First you have a sharedpreferences assistant class that's cool but your class does more than just putting and returning data and its not cool! Your class want to handle two completely separated things which is not right in object oriented programming. in getAll method you defined a fragment but you do nothing with it! i can't understand why? if you want a fragment you should returned it so after that you can pass it to fragmentManger and use it some way.
my recommended approach is to do not handle fragment in your class. just return the data as array or arrayList. also you can make your class and every method inside it static! then add your fragment to app using fragment manager (or even from layout) and inside your fragment use getAll method and initialize everything needed.
Upvotes: -1