0

我使用 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{
            
        

        }
   
 }




4

2 回答 2

0

它是这样解决的......

           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) {

               }
           });    
于 2020-09-04T07:26:37.367 回答
0

尝试像这样初始化它

SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
于 2020-09-02T16:53:43.013 回答