无限分配共享首选项,但是如何分配呢?



我正在制作一个应用程序,我有这个函数,它无限地将用户首选项分配给字符串,我不明白为什么。

爪哇文件:

public class generalSettings extends PreferenceActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.general_settings);
List<Schedule> schedules = getSchedule();  
if (schedules.isEmpty()) {
from.setSummary("--:--");
to.setSummary("--:--");
} else {
from.setSummary(schedules.get(0).getStartTime());
to.setSummary(schedules.get(0).getEndTime());
}

private List<Schedule> getSchedule() {
String cameraServiceSchedule = UserSharedPref.initializeSharedPreferencesForcameraServiceSchedule(getApplicationContext()).getString(UserSharedPref.cameraServiceSchedule, "");
String[] scheduleStrings = cameraServiceSchedule.split(";");
List<Schedule> schedules = new ArrayList<>();
Log.d("TESTa:",cameraServiceSchedule);
for (String scheduleString : scheduleStrings) {
String[] times = scheduleString.split(",");
if (times.length == 2) {
try {
Log.d("TEST:","adding only 1");
schedules.add(new Schedule(Integer.parseInt(times[0]), Integer.parseInt(times[1])));
break;
} catch (NumberFormatException ex) {
Log.d("TEST:","getting a NumberFormatException");
ex.printStackTrace();
}
}
}
return schedules;
}
}
}

时间表.java:

public class Schedule {
private  Integer startTime;
private  Integer endTime;
public Schedule() {
}
public Schedule(Integer startTime, Integer endTime) {
this.startTime = startTime;
this.endTime = endTime;
}
public  Integer getStartTime() {
return startTime;
}
public  void setStartTime(Integer startTime) {
this.startTime = startTime;
}
public  Integer getEndTime() {
return endTime;
}
public void setEndTime(Integer endTime) {
this.endTime = endTime;
}
}

当我记录此2018-11-01 17:19:27.445 4346-4346/? D/TESTa:: 891,1131;时,在不同时间连续打印,直到应用程序运行。

因此,Testa 正在无限期地打印,这是刚刚从用户首选项文件初始化的cameraServiceSchedule的日志记录输出,但是为什么,它甚至没有一次又一次地初始化循环。 当我这样做时,同一个对象schedules.get(0)一次又一次地被覆盖,List<Schedule> schedules = getSchedule();我也记录了这个。有什么解决方法吗?

使用此方法 这肯定会起作用

这是你的主要活动

/**
* Created by harkal on 01-11-2018.
*/
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ScheduleData scheduleData = new ScheduleData(this);
if(scheduleData.getSchedule() == null){
//set schedule here
scheduleData.setSchedule(new Schedule(0, 0)); //set the schedule here i have put 0 just like that
}else{
//get the data and use it
int startTime = scheduleData.getSchedule().getStartTime(); //this is the starting time use it how ever you want
int endTime = scheduleData.getSchedule().getEndTime(); //similarly this is the ending time use it how ever you want
}
}
}

这是您的共享首选项类

/**
* Created by harkal on 01-11-2018.
*/

public class ScheduleData {
private SharedPreferences sharedPreferences;
private SharedPreferences.Editor editor;
private static final String SCHEDULE_DATA = "SCHEDULE_DATA";
private static final String SCHEDULE = "SCHEDULE";
public ScheduleData(Context context) {
sharedPreferences = context.getSharedPreferences(SCHEDULE_DATA , 0);
editor = sharedPreferences.edit();
}
public void setSchedule(Schedule schedule) {
Gson gson = new Gson();
String json = gson.toJson(schedule);
editor.putString(SCHEDULE, json);
editor.commit();
}
public Schedule getSchedule() {
Gson gson = new Gson();
String json = sharedPreferences.getString(SCHEDULE, null);
return gson.fromJson(json, Schedule.class);
}
}

这是计划对象文件

/**
* Created by harkal on 01-11-2018.
*/
public class Schedule {
private int startTime;
private int endTime;
public Schedule() {
}
public Schedule(int startTime, int endTime) {
this.startTime = startTime;
this.endTime = endTime;
}
public int getStartTime() {
return startTime;
}
public void setStartTime(int startTime) {
this.startTime = startTime;
}
public int getEndTime() {
return endTime;
}
public void setEndTime(int endTime) {
this.endTime = endTime;
}
}

我用过谷歌的 gson 库 只需将此行粘贴到您的应用 gradle 文件中即可

implementation 'com.google.code.gson:gson:2.8.5'

希望对您有所帮助 快乐的编码:)

如果有任何问题 莱姆知道

最新更新