簡體   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