繁体   English   中英

如何在日历活动后重新定义“星期几”片段

[英]How to re-date 'day of the week' fragments after a Calendar activity

我正在使用代表一周中几天的7个片段,我可以在启动时成功约会我的片段。 我无法找到解决方案的问题是; 我想从我的日历活动中选择一个新日期之后重新计算每个片段的日期(我所选日期左边的片段将是前几天,而我所选日期右侧的片段将在未来)。 感谢任何想法/帮助:)

我已经搜索了这个网站,寻找近一个月的灵感

这是我的一个片段;

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();
            }
        }
    }
}

它工作正常,并从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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM