Skip to content Skip to sidebar Skip to footer

First Time Sharedpreferences Use With Gridview

This is my Apps Screenshoot : There is two button in that GridView : +/-. So what im gonna try is when i press '+' button or '-' button, the quantity is store in SharedPreferences

Solution 1:

you are using custom array list of object than set propertie to it and on back press of application you can jsonify your object to string and store it in shared preference and at activity on create you can regenerate your object getting from shared preference. in activity onCreate()

SharedPreferencesmSettings= PreferenceManager.getDefaultSharedPreferences
                (Dashboard.this);
        Stringdata= mSettings.getString("data", "");
        /* Should Activity Check for Updates Now? */if ((data.equals(""))) {
//do nothing data is not in shared preference



        }
        else {
            //data is there convert To object
            data=mSettings.getString("data", "");
            TypelistType=newTypeToken<ArrayList<ListRowItem>>() {
            }.getType();
            ArrayList<ListRowItem> listRowItems = newGson().fromJson(data, listType);
//setAdapter coming for arrayList as usual.
        }

and in backPressed you can read data from adapter and jsonify it using json and put it in sharedPreference i hope you understand the login . clear preference after data set.

Solution 2:

Your issue is here:

holder.qty.setText(store);

It is clearly pointed out in your error log:

android.content.res.Resources$NotFoundException: String resource ID 0x1 at android.content.res.Resources.getText(Resources.java:1409) at android.widget.TextView.setText(TextView.java:4943)

store is an integer and setText is trying to look for a resource with this id. Use this instead.

holder.qty.setText(store.toString());

Also, to reduce the complexity of shared preferences specific code you can use this library

Post a Comment for "First Time Sharedpreferences Use With Gridview"