0

我正在制作一个应用程序来设置日历视图上事件的详细信息。该应用程序使用 3 个活动;主要、创建事件和显示事件。
在 calendarview 中选择日期后,如果键中没有数据,则使用 setEventClick 打开使用日期作为键的创建活动。如果存在数据,它将显示活动。

但是,在我在日期中设置事件并使用 setEventClick 打开它后,我之前放置的详细信息不再存在,它再次打开创建活动。

我从阅读 SO 中了解到,非常具体和明确的问题非常重要,这是我的第一个问题,所以请让我知道如何改进我的问题以备将来使用。

主要活动

    public class MainActivity extends Activity {

    private SharedPreferences savedEvents;

    CalendarView cal;   
    public String dateSelected; 
    public String description;



    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        savedEvents = getSharedPreferences("myPrefs", MODE_PRIVATE);

        cal = (CalendarView) findViewById(R.id.calendarView1);

        cal.setOnDateChangeListener(new OnDateChangeListener() {

            @Override
            public void onSelectedDayChange(CalendarView view, int year, int month,
                int dayOfMonth) {
            month = month+1;
            Toast toast = Toast.makeText(getBaseContext(),
                    "Selected Date is\n\n"+month+" / "+dayOfMonth+" / "+year, 
                    Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.TOP|Gravity.CENTER_VERTICAL, 0, 0); //changes position Toast appears
            toast.show();   

            dateSelected = "" + month + "/" + dayOfMonth + "/" + year;

            }//end onSelectedDayChange method                       
        });//end OnDateChangeListener         

}//end OnCreate

    public void setEventClick(View v)
    {
        if (savedEvents.getString(dateSelected, "") != "") //Launches Show activity if event already exists
        {

            description = savedEvents.getString(dateSelected, "");
            savedEvents = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
            SharedPreferences.Editor editor = savedEvents.edit();
            editor.putString(dateSelected, description);
            Intent show = new Intent(this, ShowActivity.class);
            this.startActivity(show);
        }//end if

        else  //if date has no event, launches activity to create one
        {           
            SharedPreferences.Editor editor = savedEvents.edit();
            editor.putString(dateSelected, "");
            editor.putString("thisDate", dateSelected);
            Intent create = new Intent(this, CreateActivity.class);
            this.startActivity(create);         
        }//end else  
    }//end setEventClick


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;  

    }//end onCreateOptionsMenu    
}//end Main Activity 

创建活动

        public class CreateActivity extends Activity {

        public String DateSelected;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_create);       
        }//end onCreate method

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.create, menu);
            return true;
        }//end onCreateOptionsMenu method

        public void SaveClick(View v)
        {
            EditText editevent = (EditText)findViewById(R.id.editText1);
            SharedPreferences Prefs2 = getSharedPreferences("myPrefs", MODE_PRIVATE);
            Prefs2 = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
            DateSelected=Prefs2.getString("thisDate", "");
            SharedPreferences.Editor editor = Prefs2.edit();
            editor.putString(DateSelected, editevent.toString());
            finish();

        }//end SaveClick method

    }//end CreateActivity class

显示活动

public class ShowActivity extends Activity {

    public String DateSelected;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show);     

        TextView txtView2 = (TextView)findViewById(R.id.txtView);

        SharedPreferences Prefs2 = getSharedPreferences("myPrefs", MODE_PRIVATE);
        Prefs2 = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        DateSelected=Prefs2.getString("thisDate", "");
        txtView2.setText(Prefs2.getString(DateSelected, ""));               
    }//end onCreate method

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.show, menu);
        return true;
    }//end onCreateOptionsMenu method
}//end ShowActivity
4

1 回答 1

0

从您的代码中删除这一行:

Prefs2 = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

您还错过了将内容保存在sharedPreferences

editor.putString(DateSelected, editevent.toString()).commit();
于 2014-04-29T17:54:35.793 回答