我一直在尝试创建一个Android应用程序,该应用程序可以从日历中选择多个日期,并在不同的TextViews中显示选择的第一个和最后一个日期,并在Firestore数据库中更新该事件。
这是“日历活动”类 CalendarActivity.Java
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.squareup.timessquare.CalendarPickerView;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
public class CalendarActivity extends AppCompatActivity {
String eventStartDate ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final CalendarPickerView calendar_view = (CalendarPickerView)
findViewById(R.id.calendar_view);
//getting currentS
Calendar nextYear = Calendar.getInstance();
nextYear.add(Calendar.YEAR, 1);
Date today = new Date();
//add one year to calendar from todays date
calendar_view.init(today, nextYear.getTime())
.inMode(CalendarPickerView.SelectionMode.RANGE);
//action while clicking on a date
calendar_view.setOnDateSelectedListener(new
CalendarPickerView.OnDateSelectedListener() {
@Override
public void onDateSelected(Date date) {
Toast.makeText(getApplicationContext(),"Selected Date is : " +date.toString(),Toast.LENGTH_SHORT).show();
eventStartDate=date.toString();
}
@Override
public void onDateUnselected(Date date) {
//...
}
});
//fetch dates
final List<Date> dates = calendar_view.getSelectedDates();
//final int eventEndDateIndex = dates.lastIndexOf(dates);
//eventStartDate = dates.get(0);
//eventEndDate= dates.get(eventEndDateIndex);
//Displaying all selected dates while clicking on a button
Button btn_show_dates = (Button) findViewById(R.id.btn_show_dates);
btn_show_dates.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int j=0;
for (int i = 0; i< calendar_view.getSelectedDates().size();i++){
//here you can fetch all dates
Toast.makeText(getApplicationContext(),calendar_view.getSelectedDates().get(i).toString(),Toast.LENGTH_SHORT).show();
}
}
});
}
}
这是布局文件 activity_calendar.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".CalendarActivity">
<com.squareup.timessquare.CalendarPickerView
android:id="@+id/calendar_view"
android:layout_width="match_parent"
android:layout_height="467dp"
android:layout_above="@+id/btn_show_dates"
android:layout_alignParentTop="true"
android:background="#FFFFFF"
android:clipToPadding="false"
android:scrollbarStyle="outsideOverlay" />
<Button
android:id="@+id/btn_show_dates"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Display Dates"
android:background="@color/colorPrimary"
android:textColor="#FFFFFF"/>
<TextView
android:id="@+id/eventStartDate"
android:layout_width="107dp"
android:layout_height="23dp"
android:text="Event Start Date"
tools:layout_editor_absoluteX="16dp"
tools:layout_editor_absoluteY="472dp" />
<TextView
android:id="@+id/eventEndDate"
android:layout_width="116dp"
android:layout_height="23dp"
android:text="Event End Date"
tools:layout_editor_absoluteX="252dp"
tools:layout_editor_absoluteY="472dp" />
</android.support.constraint.ConstraintLayout>
答案 0 :(得分:0)
您有一个选定日期的列表作为日期对象:
calendar_view.getSelectedDates();
使用比较功能,例如:
Collections.sort(your_dates, new Comparator<Date>() {
@Override
public int compare(Date d1, Date d2) {
if(d1.getTime() > d2.getTime())
return 1;
else if(d1.getTime() < d2.getTime())
return -1;
return 0;
}
});
然后,您可以访问列表的第一个和最后一个元素。