Question

I'm making an app to set details for events on a Calendarview. The app uses 3 activities; main, create event, and show event.
Once a date is selected in calendarview, setEventClick is used to open Create activity using the date as a key if no data is present for the key already. If there is data present it will Show activity.

However after I set an event in a date and use setEventClick to open it, the details I previously placed are no longer there, and it opens the create activity again.

I know from reading on SO that being very specific and clear with questions is very important, this is my first question so please let me know how I can improve my questions for future use.

MainActivity

    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 

CreateActivity

        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

ShowActivity

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
Was it helpful?

Solution

Remove this line from your code:

Prefs2 = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

You also missed to save the stuffs in sharedPreferences:

editor.putString(DateSelected, editevent.toString()).commit();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top