1

I have a Calendar activity. When the user selects a date, I would like the TextView under the calendar to display all events the user has stored for that date. Under the TextView is a button that takes the user to the activity where they create the event. The button on the Event Creation Activity uses fileOutputStream to save a txt file containing entered information. My issue is reading that info into the TextView on the Calendar Activity. I have the code written for the read, but when I try to point it to the directory created by the fileOutput on EventCreateActivity, I get an error "EventCreateActivity is not an enclosing class." I believe it is an enclosing class, as it has nested classes, correct? What can I do here that requires the least amount of restructuring?

Here is my CalendarActivity:

public class CalendarActivity extends AppCompatActivity {

CalendarView calendar;
Button createEvent;
public static String createEventDate;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_calendar);

    calendar = (CalendarView)findViewById(R.id.calendar);
    calendar.setOnDateChangeListener(new CalendarView.OnDateChangeListener(){
        @Override
        public void onSelectedDayChange(CalendarView view, int year, int month, int dayOfMonth){
            createEventDate = (month+"."+dayOfMonth+"."+year);
            createEvent.setText("Create Event for "+createEventDate);
            File directory = EventCreateActivity.this.getFilesDir().getAbsoluteFile();
            File[] dateFile = directory.listFiles();
            if (dateFile.length > 0){
                fillEventList();
            }else{
                noEventToday();
            }
        }
    });

    createEvent = (Button)findViewById(R.id.eventCreateButton);
    createEvent.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent toEventCreateActivity = new Intent(CalendarActivity.this, EventCreateActivity.class);
            startActivity(toEventCreateActivity);
        }
    });
}

public void fillEventList (){
    TextView eventList = (TextView)findViewById(R.id.eventList);
    try {
        String message = createEventDate;
        FileInputStream fileInput = openFileInput(message);
        InputStreamReader inputStreamReader = new InputStreamReader(fileInput);
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
        StringBuffer stringBuffer = new StringBuffer();

        while ((message = bufferedReader.readLine())!=null){
            stringBuffer.append(message+"/n");
        }
        eventList.setText(stringBuffer.toString());
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

public void noEventToday(){
    TextView eventList = (TextView)findViewById(R.id.eventList);
    eventList.setText("Nothing scheduled for today.");
}
}

here is my EventCreateActivity:

public class EventCreateActivity extends AppCompatActivity {

String textViewText = CalendarActivity.createEventDate;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_event_create);

    TextView titleTextView = (TextView)findViewById(R.id.titleTextView);
    titleTextView.setText("Create event for "+textViewText);

    Button createEventButton = (Button)findViewById(R.id.saveEvent);
    createEventButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            buttonSaves();
            Intent toCalendarActivity = new Intent(EventCreateActivity.this, CalendarActivity.class);
            EventCreateActivity.this.startActivity(toCalendarActivity);
        }
    });
}

public void buttonSaves () {

    TimePicker timePicker = (TimePicker)findViewById(R.id.timePicker);

    EditText entryEvent = (EditText)findViewById(R.id.entryEvent);
    EditText entryLocation = (EditText)findViewById(R.id.entryLocation);
    EditText entryCrew = (EditText)findViewById(R.id.entryCrew);

            final String timeHour = timePicker.getCurrentHour().toString();
            final String timeMinute = timePicker.getCurrentMinute().toString();;
            final String event = entryEvent.getText().toString();
            final String location = entryLocation.getText().toString();
            final String crew = entryCrew.getText().toString();

    try{
        FileOutputStream saveNewEvent1 = openFileOutput(textViewText, MODE_WORLD_READABLE);
        OutputStreamWriter saveNewEvent2 = new OutputStreamWriter(saveNewEvent1);
        try {
            saveNewEvent2.write(timeHour);
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            saveNewEvent2.write(timeMinute);
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            saveNewEvent2.write(event);
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            saveNewEvent2.write(location);
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            saveNewEvent2.write(crew);
        } catch (IOException e) {
            e.printStackTrace();
        }

        Toast.makeText(getBaseContext(), "Roger Roger", Toast.LENGTH_LONG).show();

    }catch(FileNotFoundException e){
        e.printStackTrace();
    }

            Log.i("info","The event is: "+timeHour+timeMinute+event+location+crew);

        }
    }
snzy
  • 53
  • 8
  • You can't use `EventCreateActivity.this` outside of the `EventCreateActivity` class. – OneCricketeer Jul 13 '16 at 03:45
  • If your error is at `File directory = EventCreateActivity.this.getFilesDir().getAbsoluteFile();`, then change `EventCreateActivity` to `CalendarActivity` – OneCricketeer Jul 13 '16 at 03:47
  • When I do this, the textview only displays "New Text", regardless. I believe it is inherently referencing a directory separate than the one EventCreateActivity uses? – snzy Jul 13 '16 at 03:59
  • Shouldn't be referencing a different directory. You are using a Context method. It isn't specific to an Activity – OneCricketeer Jul 13 '16 at 04:00
  • In any case, I think you should avoid `static` variables and use callbacks – OneCricketeer Jul 13 '16 at 04:01
  • If you're referring to the static variables in my EventCreateActivity, my app kept crashing when the buttonSave method was called and I found a post on here somewhere suggesting static variables for a similar use. I switched to static and it quit crashing. – snzy Jul 13 '16 at 04:08
  • Possible duplicate of [Android return data to previous activity](http://stackoverflow.com/questions/13628003/android-return-data-to-previous-activity) – OneCricketeer Jul 13 '16 at 04:37
  • It is unclear why you even need the text file. All you are trying to do is return String data – OneCricketeer Jul 13 '16 at 04:37
  • From what I understand, creating a text file is the most efficient way to keep the data accessible and manipulable while offline for a long time, as this is a calendar application. – snzy Jul 13 '16 at 04:49
  • Sure, or a SQLite database. Either way, you can get the data from the activity through an intent result and *then* save it to a file. – OneCricketeer Jul 13 '16 at 04:55
  • Once I've figured out this issue, I will be adding statements that only create the text file if the phone is offline and the event is for a day other than the current date or the next day, as users who don't have access to the internet immediately generally will within two days, i'm guessing. I have to look into SQL still, I'm using this as app as a way to learn about all this. But, from my understanding, the user has to be connected to the internet for the SQL aspects to run. Is then data lost if they aren't? – snzy Jul 13 '16 at 04:58
  • SQLite is a persistent database running on the device. It does not require a network connection. You are confusing that with a MS-SQL or MySQL server. https://developer.android.com/guide/topics/data/data-storage.html – OneCricketeer Jul 13 '16 at 05:01

0 Answers0