我使用 sharedpreferences 来存储下次比较的值,但它崩溃了。我应该为“日期更改时图像部分每天更改”的主题创建代码,这是我的逻辑。但我无法在 android studio 中进行编码。
Calendar calendar = Calendar.getInstance();
String currentDate= DateFormat.getDateInstance(DateFormat.DATE_FIELD).format(calendar.getTime());
SharedPreferences sharedPreferences = getSharedPreferences("MyPrefs", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
if(string1 == null){
string1=currentDate;
}
string1 = savedInstanceState.getString("store_data");
if(currentDate != string1)
{
string1 = currentDate;
editor.putString("store_date", string1);
editor.apply();
Log.d("jomson","hello"+string1);
maxided = savedInstanceState.getInt("store_number");
refer=FirebaseDatabase.getInstance().getReference().child("daily").child(String.valueOf(maxided));
refer.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
String mypic = snapshot.child("pic").getValue().toString();
Picasso.get().load(mypic).into(image1);
editor.putString("store_number", String.valueOf(maxided+1));
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}else{
}
}