I'm using a 'ListView` to show all the system apps with their names, icons, and switch, and wants to save the state of the switch, it is saving the state if I check it and closes the app, but if I scroll down the list, the switch loses its state.
I'm expecting it to save the switch state in whatever situation, but it loses its state after scrolling down the list This is my code:
public class system_apps extends Fragment {
private static final String PREFERENCE_KEY = "AppSwitchStates";
private List<AppInfo> systemAppInfos = new ArrayList<>();
private AppListAdapter adapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_system_apps, container, false);
ListView listView = view.findViewById(R.id.system_app_list);
adapter = new AppListAdapter(requireContext(), systemAppInfos);
listView.setAdapter(adapter);
// Populate the list of system apps
systemAppInfos.clear();
systemAppInfos.addAll(getSystemAppInfos());
adapter.notifyDataSetChanged();
return view;
}
private List<AppInfo> getSystemAppInfos() {
List<AppInfo> appInfos = new ArrayList<>();
PackageManager packageManager = requireContext().getPackageManager();
Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> allAppInfos = packageManager.queryIntentActivities(mainIntent, 0);
SharedPreferences preferences = requireContext().getSharedPreferences(PREFERENCE_KEY, Context.MODE_PRIVATE);
for (ResolveInfo resolveInfo : allAppInfos) {
if ((resolveInfo.activityInfo.applicationInfo.flags & android.content.pm.ApplicationInfo.FLAG_SYSTEM) != 0) {
String appName = resolveInfo.loadLabel(getContext().getPackageManager()).toString();
boolean isSelected = preferences.getBoolean(appName, false);
Drawable appIcon = resolveInfo.loadIcon(packageManager);
AppInfo appInfo = new AppInfo(appName, appIcon, isSelected);
appInfos.add(appInfo);
}
}
return appInfos;
}
private class AppListAdapter extends ArrayAdapter<AppInfo> {
AppListAdapter(Context context, List<AppInfo> appInfos) {
super(context, R.layout.list_item_app, appInfos);
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_item_app, parent, false);
}
AppInfo appInfo = getItem(position);
if (appInfo != null) {
TextView appNameTextView = convertView.findViewById(R.id.app_name);
ImageView appIconImageView = convertView.findViewById(R.id.app_icon);
Switch appSwitch = convertView.findViewById(R.id.app_switch);
appNameTextView.setText(appInfo.appName);
appIconImageView.setImageDrawable(appInfo.appIcon);
appSwitch.setChecked(appInfo.isSelected);
// Set a listener to update the switch state
appSwitch.setOnCheckedChangeListener((buttonView, isChecked) -> {
// Update the switch state in the AppInfo object
appInfo.isSelected = isChecked;
// Save the switch state
saveSwitchState(appInfo.appName, isChecked);
// Handle any additional logic here
});
// Show the switch for system apps
appSwitch.setVisibility(View.VISIBLE);
}
return convertView;
}
}
private void saveSwitchState(String appName, boolean isChecked) {
SharedPreferences preferences = requireContext().getSharedPreferences(PREFERENCE_KEY, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean(appName, isChecked);
editor.apply();
}
private static class AppInfo {
String appName;
Drawable appIcon;
boolean isSelected;
AppInfo(String name, Drawable icon, boolean selected) {
appName = name;
appIcon = icon;
isSelected = selected;
}
}
}