How to get shared prefernce value from baseadaptor to activity : Selecting multiple items from custom list view and passing to another
list view.When I close the application,these selected items aregetting clean.
>I have used shared preference ,I could not able to get the data>
 MainActivity

         



    public class MainActivity extends Activity {
    private Button bt_inst_app;
    GridView gridView, gv_shortcut;
    List<AppList> installedApps;
    List<AppList> res = new ArrayList<AppList>();
    private String appName, appPackageName;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_layout);
        gv_shortcut = (GridView) findViewById(R.id.gv_shortcut);
        bt_inst_app = (Button) findViewById(R.id.bt_inst_app);
        bt_inst_app.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder quick_links_alert = new AlertDialog.Builder(MainActivity.this);
                quick_links_alert.setTitle("Edit Quick Links");
                LinearLayout quick_links_layout = new LinearLayout(MainActivity.this);
                quick_links_layout.setOrientation(LinearLayout.VERTICAL);
                LinearLayout.LayoutParams quick_links_layoutparams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                quick_links_layoutparams.setMargins(20, 0, 30, 0);
                gridView = new GridView(MainActivity.this);
                installedApps = getInstalledApps();
                final GridViewAdapter gridViewAdapter = new GridViewAdapter(MainActivity.this, installedApps);
                gridView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
                gridView.setAdapter(gridViewAdapter);
                gridView.setNumColumns(5);
                quick_links_alert.setView(gridView);
                quick_links_alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        final List<AppList> mArrayProducts = gridViewAdapter.getCheckedItems();
                        final QuickLinksGridViewAdaptor selected_apps = new QuickLinksGridViewAdaptor(MainActivity.this, mArrayProducts);
                        gv_shortcut.setAdapter(selected_apps);
                        gv_shortcut.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                            @Override
                            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                String s = mArrayProducts.get(position).getPackageName();
                                Intent PackageManagerIntent = getPackageManager().getLaunchIntentForPackage(s);//Here some time getting NULL
                                startActivity(PackageManagerIntent);
                            }
                        });
                    }
                }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                    }
                });
                quick_links_alert.show();

            }

            private List<AppList> getInstalledApps() {
                List<PackageInfo> packs = getPackageManager().getInstalledPackages(0);
                for (int i = 0; i < packs.size(); i++) {
                    PackageInfo p = packs.get(i);
                    //if ((isSystemPackage(p) == false)) {
                    if (((p.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) != true) {
                        appName = p.applicationInfo.loadLabel(getPackageManager()).toString();
                        Drawable icon = p.applicationInfo.loadIcon(getPackageManager());
                        appPackageName = p.applicationInfo.packageName;
                        res.add(new AppList(icon, appName, appPackageName));
                    }
                }
                return res;
            }
        });
    }
    }

    > GridViewAdapter

       public class GridViewAdapter<T> extends BaseAdapter {
    Context mContext;
    //private final int length = 9;*/
    private LayoutInflater layoutInflater;
    List<AppList> listStorage;
    MainActivity homeactivity;
    private int selectedIndex;
    private int selectedColor = Color.parseColor("#1b1b1b");
    SparseBooleanArray mSparseBooleanArray;
    SharedPreferences settings;
    public GridViewAdapter(Context context, List<AppList> customizedListView) {
        layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.mContext = context;
        listStorage = customizedListView;
        mSparseBooleanArray = new SparseBooleanArray();
    }
    public void setSelectedIndex(int ind) {
        selectedIndex = ind;
        notifyDataSetChanged();
    }
    public ArrayList<T> getCheckedItems() {
        ArrayList<T> mTempArry = new ArrayList<T>();
        for (int i = 0; i < listStorage.size(); i++) {
            if (mSparseBooleanArray.get(i)) {
                mTempArry.add((T) listStorage.get(i));
            }
        }
        return mTempArry;
    }
    @Override
    public int getCount() {
        return listStorage.size();
    }
    public Object getItem(int position) {
        return listStorage.get(position);
    }
    @Override
    public long getItemId(int position) {
        return 0;
    }
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder listViewHolder;
        if (convertView == null) {
            listViewHolder = new ViewHolder();
            LayoutInflater vi = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = vi.inflate(R.layout.add_apps_grid_item, parent, false);
            listViewHolder.textInListView = (TextView) convertView.findViewById(R.id.item_name);
            listViewHolder.imageInListView = (ImageView) convertView.findViewById(R.id.item_type);
            listViewHolder.tvPkgName = (TextView) convertView.findViewById(R.id.tvPack);
            listViewHolder.select_app = (CheckBox) convertView.findViewById(R.id.select_app);
            convertView.setTag(listViewHolder);
        } else {
            listViewHolder = (ViewHolder) convertView.getTag();
        }
        listViewHolder.textInListView.setText(listStorage.get(position).getName());
        listViewHolder.imageInListView.setImageDrawable(listStorage.get(position).getIcon());
        listViewHolder.tvPkgName.setText(listStorage.get(position).getPackageName());
        listViewHolder.select_app.setTag(position);
        listViewHolder.select_app.setChecked(mSparseBooleanArray.get(position));
        final AppList item = listStorage.get(position);
        listViewHolder.textInListView.setText(item.getName());
        SharedPreferences settings = mContext.getSharedPreferences("data", Context.MODE_PRIVATE);
        boolean Checked = settings.getBoolean(item.getName(), false);
        listViewHolder.select_app.setChecked(Checked);
        listViewHolder.select_app.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Toast.makeText(mContext, "pkg name==" + listStorage.get(position).getPackageName(), Toast.LENGTH_LONG).show();
                if (listViewHolder.select_app.isChecked() == true) {
                    SharedPreferences settings = mContext.getSharedPreferences("data", Context.MODE_PRIVATE);
                   // Toast.makeText(mContext, "You deselected " + Context.MODE_PRIVATE, Toast.LENGTH_SHORT).show();
                    mSparseBooleanArray.put((Integer) buttonView.getTag(), settings.edit().putBoolean(item.getName(), true).commit());
                   // Toast.makeText(mContext, "You selected " + item.getName(), Toast.LENGTH_SHORT).show();
                } else {
                    SharedPreferences settings = mContext.getSharedPreferences("data", Context.MODE_PRIVATE);
                    mSparseBooleanArray.put((Integer) buttonView.getTag(), settings.edit().putBoolean(item.getName(), false).commit());
                   // Toast.makeText(mContext, "You deselected " + item.getName(), Toast.LENGTH_SHORT).show();
                }
               // Toast.makeText(mContext, "Shared prefernce---->" + mContext.toString(), Toast.LENGTH_SHORT).show();

            }
        });

        return convertView;
    }
    OnCheckedChangeListener mCheckedChangeListener = new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView,
                                     boolean isChecked) {
            mSparseBooleanArray.put((Integer) buttonView.getTag(), isChecked);
        }

    };
    static class ViewHolder {
        TextView textInListView, tvPkgName;
        ImageView imageInListView;
        CheckBox select_app;
    }
     }

    > AppList

    public class AppList implements Parcelable {
    private String name;
    private String packageName;
    Drawable icon;
    private boolean selected;

    public AppList(Drawable icon, String name, String packageName) {
        this.name = name;
        this.icon = icon;
        this.packageName = packageName;
    }
    protected AppList(Parcel in) {
        name = in.readString();
        packageName = in.readString();
        selected = in.readByte() != 0;
    }
    public static final Creator<AppList> CREATOR = new Creator<AppList>() {
        @Override
        public AppList createFromParcel(Parcel in) {
            return new AppList(in);
        }

        @Override
        public AppList[] newArray(int size) {
            return new AppList[size];
        }
    };
    public String getName() {
        return name;
    }
    public String getPackageName() {
        return packageName;
    }
    public Drawable getIcon() {
        return icon;
    }
    public String toString() {
        return name;
    }
    @Override
    public int describeContents() {
        return 0;
    }
    @Override
    public void writeToParcel(Parcel parcel, int i) {
        parcel.writeString(name);
        parcel.writeString(packageName);
        parcel.writeByte((byte) (selected ? 1 : 0));
    }
    public boolean isSelected() {
        return selected;
    }
    public void setSelected(boolean selected) {
        this.selected = selected;
    }
     }
    
    QuickLinksGridViewAdaptor
    public class QuickLinksGridViewAdaptor<T> extends BaseAdapter {
    private Context context;
    //private final int length = 9;*/
    private LayoutInflater layoutInflater;
    List<AppList> listStorage;
    MainActivity homeactivity;
    private int selectedIndex;
    SparseBooleanArray mSparseBooleanArray;
    int num = 1;
    public QuickLinksGridViewAdaptor(Context context, List<AppList> customizedListView) {
        layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        listStorage = customizedListView;
        mSparseBooleanArray = new SparseBooleanArray();
    }
    public void setSelectedIndex(int ind) {
        selectedIndex = ind;
        notifyDataSetChanged();
    }
    public ArrayList<T> getCheckedItems() {
        ArrayList<T> mTempArry = new ArrayList<T>();
        for (int i = 0; i < listStorage.size(); i++) {
            if (mSparseBooleanArray.get(i)) {
                mTempArry.add((T) listStorage.get(i));
            }
        }
        return mTempArry;
    }
    @Override
    public int getCount() {
        //return 8;
        if(num*8 >= listStorage.size()){
            return listStorage.size();
        }else{
            return num*8;
        }
    }
    public Object getItem(int position) {
        return listStorage.get(position);
    }
    @Override
    public long getItemId(int position) {
        return 0;
    }
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder listViewHolder;
        if (convertView == null) {
            listViewHolder = new ViewHolder();
            convertView = layoutInflater.inflate(R.layout.add_apps_grid_item1, parent, false);
            listViewHolder.textInListView = (TextView) convertView.findViewById(R.id.item_name1);
            listViewHolder.imageInListView = (ImageView) convertView.findViewById(R.id.item_type1);
            convertView.setTag(listViewHolder);

        } else {
            listViewHolder = (ViewHolder) convertView.getTag();
        }
        listViewHolder.textInListView.setText(listStorage.get(position).getName());
        listViewHolder.imageInListView.setImageDrawable(listStorage.get(position).getIcon());
        return convertView;
    }
    static class ViewHolder {
        TextView textInListView;
        ImageView imageInListView;
    }
      }

   

