根据日期更改在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) {

               }
           });