如何解决根据日期更改在Android项目中更新图像
我使用了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{
}
}
解决方法
尝试像这样初始化它
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
,
像这样解决...
String currentDate= DateFormat.getDateInstance(DateFormat.DATE_FIELD).format(calendar.getTime());
SharedPreferences mydate = this.getSharedPreferences("mydata",Context.MODE_PRIVATE);
string1 = mydate.getString("stored",String.valueOf(DEFAULT_KEYS_DISABLE));
maxided = mydate.getInt("number",DEFAULT_KEYS_DISABLE);
if(!currentDate.equals(string1) || maxided == 0) {
string1 = currentDate;
mydate = getSharedPreferences("mydata",Context.MODE_PRIVATE);
SharedPreferences.Editor editor = mydate.edit();
editor.putString("stored",string1);
maxided=+1;
editor.putInt("number",maxided);
editor.apply();
}else{
Toast.makeText(this,"SharedPreference worked",Toast.LENGTH_SHORT).show();
}
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);
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。