Я хочу запомнить состояние кнопки переключения в просмотре списка всякий раз, когда я нажимал кнопку переключения.
Я пытался использовать общие настройки, чтобы запомнить состояние кнопки переключения на основе позиции элемента, но это не сработало. Однако я думаю, что проблема заключается в том, что, если я удаляю один из элементов из списка, позиция может измениться.
// getting the toggle button state using shared preference
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getContext());
Boolean runstopChecked = sharedPreferences.getBoolean("pref_runstop", false);
viewHolder.tbtnJobRunStop.setChecked(runstopChecked);
// Saving the position and toggle button state
viewHolder.tbtnJobRunStop.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getContext());
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt("pref_runstopposition", position);
if(isChecked)
{
runJobId = dataModel.getJobId();
editor.putBoolean("pref_runstop", true);
editor.apply();
Intent statusIntent = new Intent(getContext(), RefreshJobService.class);
statusIntent.putExtra("id", runJobId);
getContext().startService(statusIntent);
} else
{
stopJobId = dataModel.getJobId();
editor.putBoolean("pref_runstop", false);
editor.apply();
stopJob stopjob = new stopJob();
stopjob.execute();
Intent i = new Intent("stopjob");
LocalBroadcastManager.getInstance(getContext()).sendBroadcast(i);
i.putExtra("stopJobId",stopJobId);
getContext().sendBroadcast(i);
}
}
});