I want to select multiple dates in calendar in android

前端 未结 3 1899
无人及你
无人及你 2021-01-18 14:00

I want to select multiple dates in calendar,multiple select is working fine but in toast displaying only single date(first date).If I selected 4 days(1.8.14 to 4.8.14) all

相关标签:
3条回答
  • 2021-01-18 14:06

    Try below code for briefly explanation.......

    final Calendar nextYear = Calendar.getInstance();
        nextYear.add(Calendar.YEAR, 2);
    
    
         gson = new Gson();
        sharedpreferences1 = getSharedPreferences("MyPREFERENCES10005471hjsdhjsdjghjfdjksdlsasa00fdsadeetytadsadsywrerwerrw0255878762343", Context.MODE_PRIVATE);
        editor = sharedpreferences1.edit();
        calendar = (CalendarPickerView) findViewById(R.id.calendar_view);
        final String ffffd=sharedpreferences1.getString("Str",null);
        sdf = new SimpleDateFormat("dd-MM-yyyy");
        Type type = new TypeToken<ArrayList<Date>>() {}.getType();
        arrayList = gson.fromJson(ffffd, type);
        dates = new ArrayList<Date>();
        if(arrayList!=null){
            Toast.makeText(SampleTimesSquareActivity.this, "" + arrayList, LENGTH_SHORT).show();
            calendar.init(new Date(), nextYear.getTime()) //
                    .inMode(SelectionMode.MULTIPLE) //
                    .withSelectedDates(arrayList);
            for(int ii=0;ii<arrayList.size();ii++){
                Date d=arrayList.get(ii);
                String format=sdf.format(d);
                Toast.makeText(getApplicationContext(),format,Toast.LENGTH_SHORT).show();
            }
        }
        else{
            Toast.makeText(SampleTimesSquareActivity.this, "Null", LENGTH_SHORT).show();
            calendar.init(new Date(), nextYear.getTime()) //
                    .inMode(SelectionMode.MULTIPLE) //
                    .withSelectedDates(arrayList);
        }
    
    
    
        findViewById(R.id.done_button).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.d(TAG, "Selected time in millis: " + calendar.getSelectedDate().getTime());
                String toast = "Selected: " + calendar.getSelectedDate();
                 dates = (ArrayList<Date>) calendar.getSelectedDates();
                 String json=null;
                Log.i("Size",""+dates);
                for (int i = 0; i < dates.size(); i++) {
                    Date tempDate = dates.get(i);
                    String formattedDate = sdf.format(tempDate);
                    Toast.makeText(SampleTimesSquareActivity.this, "" + formattedDate, LENGTH_SHORT).show();
                     json = gson.toJson(dates);
    
                }
    
                editor.putString("Str", json);
                editor.clear();
                editor.apply();
            }
        });
    

    Thanks...take enjoy

    0 讨论(0)
  • 2021-01-18 14:12

    A quick google says there is a method in CalendarPickerView called getSelectedDates() which returns a list of dates. This is probably what you want.

    Updated link: https://github.com/square/android-times-square/blob/master/library/src/main/java/com/squareup/timessquare/CalendarPickerView.java

    0 讨论(0)
  • 2021-01-18 14:18

    Use SelectionMode.MULTIPLE

    calendar.init(today, nextYear.getTime())
            .inMode(CalendarPickerView.SelectionMode.MULTIPLE);
    
    findViewById(R.id.btn_done).setOnClickListener(new View.OnClickListener() {
    @Override public void onClick(View view) {
        Log.d("TAGGG", "Selected time in millis: " + calendar.getSelectedDate().getTime());
        String toast = "Selected: " + calendar.getSelectedDates();
        Toast.makeText(CalenderItem.this, toast, Toast.LENGTH_SHORT).show();
    }
    });
    
    0 讨论(0)
提交回复
热议问题