I am using 7 fragments that represent the days of the week, I can successfully date my fragments on startup. The problem I cannot find a solution to is; I would like to re-date each fragment after i have selected a new date from my Calendar Activity (fragments to the left of my selected date will be days prior and fragments to the right of my selected date will be in the future). Any thoughts/help will be gratefully received :)
I have scoured this site looking for inspiration for nearly a month
This is one of my fragments;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.tab_fragment1, container, false);
Button dateChangeBtn = rootView.findViewById(R.id.changeDateBtn1);
dateChangeBtn.setOnClickListener(this);
ctx = MainActivity.ctx;
return rootView;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mTodayDate1 = view.findViewById(R.id.todayDate1);
//set the date on the fragment dependant on whether the fragment is to the right or
//the left of 'Today's' fragment
int calAddAmount = TF1 - MainActivity.getAdjustedCurrentItem();
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DAY_OF_MONTH, calAddAmount);
Date date = calendar.getTime();
mTodayDate1.setText(MainActivity.getFriendlyDate(date));
}
/**
* Called when a view has been clicked.
*
* @param v The view that was clicked.
*/
@Override
public void onClick(View v) {
Intent calendarIntent = new Intent(ctx, CalendarActivity.class);
startActivityForResult(calendarIntent, CALENDAR_REQUEST );
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CALENDAR_REQUEST) {
if (resultCode == RESULT_OK) {
try {
String dateString = data.getStringExtra(CalendarActivity.CALENDAR_REPLY);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = sdf.parse(dateString);
mTodayDate1.setText(MainActivity.getFriendlyDate(date));
} catch (ParseException e) {
e.printStackTrace();
}
}
}
}
It works fine as it stands and returns a date from the Calendar activity and dates just that one fragment
I walked away from this problem and got on with something else, I came back to it and came up with this solution (it isn't pretty and I'd appreciate any assistance in making it better, but it works and it dates all my fragments for the week of the day that was chosen from the calendar)
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CALENDAR_REQUEST) {
if (resultCode == RESULT_OK) {
String dateString = data.getStringExtra(CalendarActivity.CALENDAR_REPLY);
String dayName = "";
Date date = null;
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
date = sdf.parse(dateString);
} catch (ParseException e) {
e.printStackTrace();
}
SimpleDateFormat sdfDay = new SimpleDateFormat("EEEE");
dayName = sdfDay.format(date);
//get the index of chosen day
int dayInt = getDayIndex(dayName);
//initialise all mTodayDate
mTodayDate0 = findViewById(R.id.todayDate0);//Sunday
mTodayDate1 = findViewById(R.id.todayDate1);
mTodayDate2 = findViewById(R.id.todayDate2);
mTodayDate3 = findViewById(R.id.todayDate3);
mTodayDate4 = findViewById(R.id.todayDate4);
mTodayDate5 = findViewById(R.id.todayDate5);
mTodayDate6 = findViewById(R.id.todayDate6);//Saturday
//convert to calendar object
Calendar cal = Calendar.getInstance();
cal.setTime(date);
//subtract dayInt from date chosen to give you Sunday's date
cal.add(Calendar.DAY_OF_MONTH, -dayInt);
//convert back to date object for setting the frag
Date convertedFromCalendar = cal.getTime();
//set date on frags, adding a day each time
mTodayDate0.setText(getFriendlyDate(convertedFromCalendar));
cal.setTime(convertedFromCalendar);
cal.add(Calendar.DAY_OF_MONTH, 1);
convertedFromCalendar = cal.getTime();
mTodayDate1.setText(getFriendlyDate(convertedFromCalendar));
cal.setTime(convertedFromCalendar);
cal.add(Calendar.DAY_OF_MONTH, 1);
convertedFromCalendar = cal.getTime();
mTodayDate2.setText(getFriendlyDate(convertedFromCalendar));
cal.setTime(convertedFromCalendar);
cal.add(Calendar.DAY_OF_MONTH, 1);
convertedFromCalendar = cal.getTime();
mTodayDate3.setText(getFriendlyDate(convertedFromCalendar));
cal.setTime(convertedFromCalendar);
cal.add(Calendar.DAY_OF_MONTH, 1);
convertedFromCalendar = cal.getTime();
mTodayDate4.setText(getFriendlyDate(convertedFromCalendar));
cal.setTime(convertedFromCalendar);
cal.add(Calendar.DAY_OF_MONTH, 1);
convertedFromCalendar = cal.getTime();
mTodayDate5.setText(getFriendlyDate(convertedFromCalendar));
cal.setTime(convertedFromCalendar);
cal.add(Calendar.DAY_OF_MONTH, 1);
convertedFromCalendar = cal.getTime();
mTodayDate6.setText(getFriendlyDate(convertedFromCalendar));
}
}
}
//Returns the index number of the given day
public int getDayIndex(String aDayName){
String[] daysOfTheWeek = new String[] {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
int dayInt = Arrays.asList(daysOfTheWeek).indexOf(aDayName);
return dayInt;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.