我需要根据事件日期显示和隐藏复选框

时间:2015-09-15 09:58:39

标签: android android-layout date android-activity

我需要根据事件日期显示和隐藏复选框....我有3个复选框在事件日,前2天,前一周在我的代码中调用....添加事件,添加时日期本身应该计算间隔。根据当前日期和事件日期之间的间隔,我需要更改复选框的可见性。例如,如果间隔是2天....我只需要显示事件日和复选框前2天....为此我需要计算interval.where我需要添加复选框的代码....请帮我找出代码...

    protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_occasion);

            context = getApplicationContext();

            seekBar_startTime = (SeekBar) findViewById(R.id.seekBar1);
            seekBar_startTime.setProgress(0);
            seekBar_startTime.setMax(1440); //minutes in 24 hours(24*60)
            seekBar_startTime.setProgress(1);
            textView_startTime = (TextView) findViewById(R.id.textView1);

            seekBar_endTime = (SeekBar) findViewById(R.id.seekBar2);
            seekBar_endTime.setProgress(0);
            seekBar_endTime.setMax(1440); //minutes in 24 hours(24*60)
            seekBar_endTime.setProgress(1);
            textView_endTime = (TextView) findViewById(R.id.textView2);

            btn_Date = (Button) findViewById(R.id.event_date_button);
            event_Place = (TextView) findViewById(R.id.Enter_Place);
            custom_Text = (TextView) findViewById(R.id.Custom_Text);
            checkBox_onEventDay=(CheckBox) findViewById(R.id.cb_onEventDay);
            checkBox_2DaysBefore=(CheckBox) findViewById(R.id.cb_2DaysBefore);
            checkBox_aWeekBefore=(CheckBox) findViewById(R.id.cb_aWeekBefore);

            btn_create_button = (Button) findViewById(R.id.create_button);
            sharedpreferences = context.getSharedPreferences(Constants.SHARED_PREFERENCE_NAME, Context.MODE_PRIVATE);
            dbHelper = new DatabaseHelper(context);



            btn_Date.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {

                    DatePickerFragment newFragment = new DatePickerFragment();
                    newFragment.show(getFragmentManager(), "Occasion Date");
                    //Calendar eventDay = Calendar.getInstance();
 try {
                        String eventDate = btn_Date.getText().toString();
                        DateFormat date = new SimpleDateFormat("dd-M-yyyy");
                        Date date1 = date.parse(eventDate);
                        DateFormat convertDate = new SimpleDateFormat(" yyyy-MM-dd hh:mm:ss");
                        eventDate = convertDate.format(date1);
                        eventMO.setEventDate(eventDate);



                            Date currentDate = new Date();

                            long diff = date1.getTime()-currentDate.getTime();
                            long seconds = diff / 1000;
                            long minutes = seconds / 60;
                            long hours = minutes / 60;
                            long daysBefore = hours / 24;
hours%=24;
                    } catch (ParseException e) {

                        e.printStackTrace();
                    }
                        if (daysBefore >= 7) {
                            if ((daysBefore == 7 && hours > 9) != true) {
                                checkBox_aWeekBefore.setVisibility(View.VISIBLE);
                            }
                            checkBox_2DaysBefore.setVisibility(View.VISIBLE);

                            checkBox_onEventDay.setVisibility(View.VISIBLE);
                        }
                        else if (daysBefore >= 2) {
                            if ((daysBefore == 2 && hours > 9) != true) {
                                checkBox_aWeekBefore.setVisibility(View.INVISIBLE);
                                //checkBox_2DaysBefore.setVisibility(View.VISIBLE);
                            }
                            //checkBox_onEventDay.setVisibility(View.VISIBLE);
                        }
                        else if (daysBefore >= 1) {
                            checkBox_aWeekBefore.setVisibility(View.INVISIBLE);
                            checkBox_2DaysBefore.setVisibility(View.INVISIBLE);
                            //checkBox_onEventDay.setVisibility(View.VISIBLE);
                        }
                        }


                    });
    /eventid get from OccasionFragment here
            Bundle bundle = this.getIntent().getExtras();
            if (bundle != null) {

                Long eventId1 = bundle.getLong("EventID");
                eventId2 = eventId1;
                String eventName = bundle.getString("EventName");
                custom_Text.setText(eventName);
                String eventPlace = bundle.getString("EventPlace");
                event_Place.setText(eventPlace);
                String eventDate = bundle.getString("EventDate");
                try {
                    //String eventDate = bundle.getString("EventDate");
                    //time string removed from date here
                    String result = eventDate.split(" ")[0];
                    DateFormat sourceDate = new SimpleDateFormat("yyyy-MM-dd");
                    // parse the date string into Date object
                    Date date = sourceDate.parse(result);
                    DateFormat convertDate = new SimpleDateFormat("dd-M-yyyy");
                    // format the date into another format
                    result = convertDate.format(date);
                    btn_Date.setText(result);


                } catch (ParseException e) {

                    e.printStackTrace();
                }
                //Event Start Time and Event End Time string added before the time here
                String eventStart = bundle.getString("EventStart");
                textView_startTime.setText("Event Start Time :" + eventStart);
                String eventEnd = bundle.getString("EventEnd");
                textView_endTime.setText("Event End Time :" + eventEnd);

            }

            btn_create_button.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {

                    doOnCLickAndOnOptionSelected();

                }
            });


        }

        @SuppressLint("ValidFragment")
        public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {

            @Override
            public Dialog onCreateDialog(Bundle savedInstanceState) {
                // Use the current date as the default date in the picker
                final Calendar c = Calendar.getInstance();
                int year = c.get(Calendar.YEAR);
                int month = c.get(Calendar.MONTH);
                int day = c.get(Calendar.DAY_OF_MONTH);

                // Create a new instance of DatePickerDialog and return it
                return new DatePickerDialog(getActivity(), this, year, month, day);
            }

            public void onDateSet(DatePicker view, int year, int month, int day) {
                Date = new StringBuilder().append(year).append("-").append(month + 1).append("-").append(day).append(" ").toString();
                btn_Date.setText(new StringBuilder().append(day).append("-").append(month + 1).append("-").append(year).append(" ").toString());
            }
        }

    }

我在点击监听器上的btn_date中尝试了这个代码,以计算添加日期时的间隔...但它不起作用....我需要在添加日期本身时可见和不可见..请帮帮我......

添加事件包括事件名称,地点,日期,开始时间,结束时间......此后我只有3个复选框。然后保存按钮以保存事件

1 个答案:

答案 0 :(得分:1)

删除小时声明,daysBefore ... 像这样使用......这就是问题

  hours = minutes / 60;
  daysBefore = hours / 24;

你的计算是错误的,以获得剩余的日子。

diff = event_date - current_date

   long diff = date1.getTime() - currentDate.getTime() ;



      try {
                    String eventDate = btn_Date.getText().toString();
                    DateFormat date = new SimpleDateFormat("dd-M-yyyy");
                    Date date1 = date.parse(eventDate);
                    DateFormat convertDate = new SimpleDateFormat(" yyyy-MM-dd hh:mm:ss");
                    eventDate = convertDate.format(date1);
                    eventMO.setEventDate(eventDate);



                        Date currentDate = new Date();
                        //This is remainder kind of app right
                        //long diff = currentDate.getTime() - date1.getTime();

                        //days_remaining - current
                        long diff = date1.getTime() - currentDate.getTime() ;
                        long seconds = diff / 1000;
                        long minutes = seconds / 60;
                        hours = minutes / 60;
                        daysBefore = hours / 24;

                      //you need get remaining hours
                       hours %= 24;

                   if (daysBefore >= 7) {
                        if ((daysBefore == 7 && hours > 9) != true) {
                            checkBox_aWeekBefore.setVisibility(View.VISIBLE);
                        }
                        checkBox_2DaysBefore.setVisibility(View.VISIBLE);

                        checkBox_onEventDay.setVisibility(View.VISIBLE);
                    }
                    else if (daysBefore >= 2) {
                        if ((daysBefore == 2 && hours > 9) != true) {
                            checkBox_aWeekBefore.setVisibility(View.INVISIBLE);
                            //checkBox_2DaysBefore.setVisibility(View.VISIBLE);
                        }
                        //checkBox_onEventDay.setVisibility(View.VISIBLE);
                    }
                    else if (daysBefore >= 1) {
                        checkBox_aWeekBefore.setVisibility(View.INVISIBLE);
                        checkBox_2DaysBefore.setVisibility(View.INVISIBLE);
                        //checkBox_onEventDay.setVisibility(View.VISIBLE);
                    }                    
                } catch (ParseException e) {

                    e.printStackTrace();
                }