I'm working on a project where I want to make an app which has some kind of calendar.
Because I think a datepicker is a handy tool, I want to uses this. When the date is picked, the CalendarUtils.selectedDate has to change to in order to change the week view. To let this work, I have to make a LocalDate from a string (output datepicker).
Unfortunately my app only makes it 50% of the times when the datepicker is used. Sometimes it works, sometimes it doesn't. I'm fairy certain it has something to do with this line
LocalDate localDate = LocalDate.parse(date_string, formatter);
because when I just use a string to display the date (and skip the updating weekview) it works all the time.
Someone who can help me or know a way to bypass this problem?
My code:
package codewithcal.au.calendarappexample;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;
import android.app.DatePickerDialog;
import java.time.LocalDate;
import java.util.ArrayList;
import static codewithcal.au.calendarappexample.CalendarUtils.daysInWeekArray;
import java.time.format.DateTimeFormatter;
import java.time.LocalDateTime;
public class WeekViewActivity extends AppCompatActivity implements CalendarAdapter.OnItemListener
{
private TextView monthYearText;
private RecyclerView calendarRecyclerView;
private ListView eventListView;
TextView dateView;
DatePickerDialog picker;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_week_view);
initWidgets();
CalendarUtils.selectedDate = LocalDate.now();
setWeekView();
// datepicker
dateView = findViewById(R.id.monthYearTV);
dateView.setOnClickListener(v -> {
picker = new DatePickerDialog(WeekViewActivity.this,
(view, year, monthOfYear, dayOfMonth) -> {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
String date_string = dayOfMonth + "/" + (monthOfYear + 1) + "/" + year;
LocalDate localDate = LocalDate.parse(date_string, formatter);
CalendarUtils.selectedDate = localDate;
setWeekView();
dateView.setText(localDate.format(formatter));
}, CalendarUtils.selectedDate.getYear(), CalendarUtils.selectedDate.getMonthValue()-1, CalendarUtils.selectedDate.getDayOfMonth());
picker.show();
});
}
private void initWidgets()
{
calendarRecyclerView = findViewById(R.id.calendarRecyclerView);
monthYearText = findViewById(R.id.monthYearTV);
eventListView = findViewById(R.id.eventListView);
}
private void setWeekView()
{
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("dd/MM/yyyy");
LocalDateTime now = LocalDateTime.now();
monthYearText.setText(dtf.format(now));
ArrayList<LocalDate> days = daysInWeekArray(CalendarUtils.selectedDate);
CalendarAdapter calendarAdapter = new CalendarAdapter(days, this);
RecyclerView.LayoutManager layoutManager = new GridLayoutManager(getApplicationContext(), 7);
calendarRecyclerView.setLayoutManager(layoutManager);
calendarRecyclerView.setAdapter(calendarAdapter);
setEventAdpater();
}
public void previousWeekAction(View view)
{
CalendarUtils.selectedDate = CalendarUtils.selectedDate.minusWeeks(1);
setWeekView();
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("dd/MM/yyyy");
monthYearText.setText(dtf.format(CalendarUtils.selectedDate));
}
public void nextWeekAction(View view)
{
CalendarUtils.selectedDate = CalendarUtils.selectedDate.plusWeeks(1);
setWeekView();
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("dd/MM/yyyy");
monthYearText.setText(dtf.format(CalendarUtils.selectedDate));
}
@Override
public void onItemClick(int position, LocalDate date)
{
CalendarUtils.selectedDate = date;
setWeekView();
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("dd/MM/yyyy");
monthYearText.setText(dtf.format(date));
}
@Override
protected void onResume()
{
super.onResume();
setEventAdpater();
}
private void setEventAdpater()
{
ArrayList<Event> dailyEvents = Event.eventsForDate(CalendarUtils.selectedDate);
EventAdapter eventAdapter = new EventAdapter(getApplicationContext(), dailyEvents);
eventListView.setAdapter(eventAdapter);
}
public void newEventAction(View view)
{
startActivity(new Intent(this, EventEditActivity.class));
}
}
package codewithcal.au.calendarappexample;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.util.List;
public class EventAdapter extends ArrayAdapter<Event>
{
public EventAdapter(@NonNull Context context, List<Event> events)
{
super(context, 0, events);
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent)
{
Event event = getItem(position);
if (convertView == null)
convertView = LayoutInflater.from(getContext()).inflate(R.layout.event_cell, parent, false);
TextView eventCellTV = convertView.findViewById(R.id.eventCellTV);
String eventTitle = event.getName() +" "+ CalendarUtils.formattedTime(event.getTime());
eventCellTV.setText(eventTitle);
return convertView;
}
}
A screen from how the interface looks:
