为什么我的SharedPreference对象没有在Android中显示更新结果?

时间:2014-05-22 13:41:19

标签: android android-asynctask sharedpreferences

我正在Android中编写代码以保存日期选择器中的所选日期。我在Sharedpreference对象中保存日期,但在关闭和打开应用程序后显示最新结果。

我认为必须有刷新活动需要显示来自SharedPreference对象的最新保存结果,所以我使用了AsyncTask并试图获得最新结果但是应用程序没有显示最新结果,每当我关闭应用程序并重新 - 再次打开应用程序,然后我可以看到最新保存的结果。

我的代码:

package com.example.date_pick;

import java.util.Calendar;
import java.util.concurrent.ExecutionException;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.Dialog;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.DatePicker.OnDateChangedListener;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity{

DatePicker pickerDate;
TextView info;

Button btn;


Button btn12;


@Override
protected void onCreate(Bundle savedInstanceState)
{
     super.onCreate(savedInstanceState);

     setContentView(R.layout.activity_main);

     btn = (Button)findViewById(R.id.btn);

     info = (TextView)findViewById(R.id.test);

  final SharedPreferences pref = getApplicationContext().getSharedPreferences("Demo", 0);


   btn.setOnClickListener(new View.OnClickListener() 
   {

            @Override
            public void onClick(View v) 
            {

                            final Dialog dia = new Dialog(MainActivity.this);

                            dia.requestWindowFeature(dia.getWindow().FEATURE_NO_TITLE);       

                            dia.setContentView(R.layout.mydate);


                            btn12 = (Button)dia.findViewById(R.id.btn1);

                            pickerDate = (DatePicker)dia.findViewById(R.id.pickerdate);

                            Calendar today = Calendar.getInstance();

                            pickerDate.init(
                                                today.get(Calendar.YEAR), 
                                                today.get(Calendar.MONTH), 
                                                today.get(Calendar.DAY_OF_MONTH), 
                                                new OnDateChangedListener(){

                                                 @Override
                                                 public void onDateChanged(DatePicker view, int year, int monthOfYear,int dayOfMonth) 
                                                 {

                                                            Toast.makeText(getApplicationContext(), "onDateChanged", Toast.LENGTH_SHORT).show();


                                                                                            Editor et = pref.edit();

                                                                                            et.remove("year");

                                                                                            et.remove("month");

                                                                                            et.remove("day");

                                                                                            et.commit();

                                                                                            et.putInt("year", year);

                                                                                            et.putInt("month", monthOfYear);

                                                                                            et.putInt("day", dayOfMonth);

                                                                                            et.commit();

                                                 }});


                            btn12.setOnClickListener(new View.OnClickListener() {

                                            @Override
                                            public void onClick(View v) {

                                                            dia.dismiss();

                                            }
                            });

                            dia.show();

            }
   });

         MyShared ms = new MyShared();

            String save_date;
            try {

                            save_date = ms.execute().get();

                            info.setText("Result : "+save_date);
            } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
            } catch (ExecutionException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
            }


   }


   private class MyShared extends AsyncTask<String ,Void, String>
   {

                            String save_date = null;

                            SharedPreferences pref = getApplicationContext().getSharedPreferences("Demo", 0);
                            @Override
                            protected  String  doInBackground(String... params) 
                            {


                                            int year = pref.getInt("year", 0);

                                              int month =  pref.getInt("month", 0);

                                              int day =   pref.getInt("day", 0);

                                                            String data = String.valueOf(day+"/"+month+"/"+year);

                                                            return data;

                            }
            }
 }

如何从SharedPreference对象获取最新保存的结果。请告诉我,建议我一些好的解决方案。

2 个答案:

答案 0 :(得分:1)

查看你的代码,你初始化onDateChanged然后执行asynctask。

你得到第一个结果。

然后onDateChanged更改首选项,您需要再次调用它。

代码(未经测试)

import java.util.Calendar;
import java.util.concurrent.ExecutionException;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.Dialog;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.DatePicker.OnDateChangedListener;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity{

    DatePicker pickerDate;
    TextView info;

    Button btn;


    Button btn12;


    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        btn = (Button)findViewById(R.id.btn);

        info = (TextView)findViewById(R.id.test);

        final SharedPreferences pref = getApplicationContext().getSharedPreferences("Demo", 0);

        new MyShared().execute();


        btn.setOnClickListener(new View.OnClickListener()
        {

            @Override
            public void onClick(View v)
            {

                final Dialog dia = new Dialog(MainActivity.this);

                dia.requestWindowFeature(dia.getWindow().FEATURE_NO_TITLE);

                dia.setContentView(R.layout.mydate);


                btn12 = (Button)dia.findViewById(R.id.btn1);

                pickerDate = (DatePicker)dia.findViewById(R.id.pickerdate);

                Calendar today = Calendar.getInstance();

                pickerDate.init(
                        today.get(Calendar.YEAR),
                        today.get(Calendar.MONTH),
                        today.get(Calendar.DAY_OF_MONTH),
                        new OnDateChangedListener(){

                            @Override
                            public void onDateChanged(DatePicker view, int year, int monthOfYear,int dayOfMonth)
                            {

                                Toast.makeText(getApplicationContext(), "onDateChanged", Toast.LENGTH_SHORT).show();


                                Editor et = pref.edit();

                                et.remove("year");

                                et.remove("month");

                                et.remove("day");

                                et.commit();

                                et.putInt("year", year);

                                et.putInt("month", monthOfYear);

                                et.putInt("day", dayOfMonth);

                                et.commit();

                                new MyShared().execute();

                            }});


                btn12.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {

                        dia.dismiss();

                    }
                });

                dia.show();

            }
        });
    }


    private class MyShared extends AsyncTask<String ,Void, String>
    {
        SharedPreferences pref = getApplicationContext().getSharedPreferences("Demo", 0);
        @Override
        protected  String  doInBackground(String... params)
        {


            int year = pref.getInt("year", 0);

            int month =  pref.getInt("month", 0);

            int day =   pref.getInt("day", 0);

            String data = String.valueOf(day+"/"+month+"/"+year);

            return data;

        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            info.setText(s);
        }
    }
}

答案 1 :(得分:0)

dia.dismiss();执行你的asynctask之后。即,在您关闭DatePicker后,将所有代码从MyShared ms = new MyShared()...写到catch(ExecuteException e){}...

这将关闭对话框并从sharedPreferences读取数据并更新信息。