2016-05-16 4 views
0

In meiner Anwendung verwende ich this library für benutzerdefinierte Kalenderansicht. Klicken Sie auf ein beliebiges Datum, an dem ich dieses Datum durch gemeinsame Einstellungen an die nächste Aktivität weiterleite. Ich kann das Datum in der nächsten Aktivität anzeigen. Nun, was ich will, ist, gibt es zwei Tasten, eine ist "Previous" und eine andere "Next". wenn ich auf irgendeinen Knopf klicke, muss Datum entsprechend Anforderung ändern.vorheriger/nächster Tag Datum auf Taste drücken, nachdem Datum von vorheriger Aktivität mit SharedPreferences erhalten wurde Android

Onclick Datumscode ist

calendarView.setOnDateClickListener(new CalendarView.OnDateClickListener() { 
     @Override 
     public void onDateClick(@NonNull Date selectedDate) { 
      SimpleDateFormat df_new = new SimpleDateFormat("dd-MM-yyyy", Locale.getDefault()); 
      String newselectedDate=df_new.format(selectedDate); 
      Toast.makeText(getApplicationContext(), "You selected" + newselectedDate + " : Date", Toast.LENGTH_SHORT).show(); 

      // SharedPreferences for sharing the Date with Next Activity, Added by Tara 
      SimpleDateFormat s_day=new SimpleDateFormat("dd",Locale.getDefault()); String sdd=s_day.format(selectedDate); 
      SimpleDateFormat s_month=new SimpleDateFormat("MM",Locale.getDefault());String smm=s_month.format(selectedDate); 
      SimpleDateFormat s_year=new SimpleDateFormat("yyyy",Locale.getDefault());String syy=s_year.format(selectedDate); 
      SharedPreferences spf= getSharedPreferences("myprfs", Context.MODE_PRIVATE); 
      SharedPreferences.Editor spe = spf.edit(); 
      // spe.putString("sdate", String.valueOf(selectedDate)); 
      spe.putString("sday", String.valueOf(sdd)); 
      spe.putString("smonth", String.valueOf(smm)); 
      spe.putString("syear", String.valueOf(syy)); 
      Log.d("Day is", String.valueOf(sdd)); 
      Log.d("Month is", String.valueOf(smm)); 
      Log.d("Year is", String.valueOf(syy)); 

      spe.commit(); 


      Intent i_available_slot=new Intent(BookAnAppoinment.this, ChangeDate.class); 
      startActivity(i_available_slot); 
     } 
    }); 

und nächste Aktivität, wo ich durch gemeinsame Vorliebe am Abrufen von Datum ist

SharedPreferences spf=getSharedPreferences("myprfs", Context.MODE_PRIVATE); 
    String id1 = spf.getString("sday", "no value"); 
    String id2 = spf.getString("smonth", "no value"); 
    String id3 = spf.getString("syear", "no value"); 

    String sdate=id1+"-"+id2+"-"+id3; 
    // d.setText(sdate); 

    final SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy"); 
    final String formattedDate = df.format(sdate); 

    d.setText(formattedDate); 

    try { 
     Date date=df.parse(sdate); 
     Calendar cal = df.getCalendar(); 
    } catch (ParseException e) { 
     e.printStackTrace(); 
    } 

    n.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 

     } 
    }); 
    p.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 

     } 
    }); 

I try/catch-Block geschrieben, aber nicht in der Lage zu Code wie pro meinem Anforderung. bitte hilf mir.

Antwort

0

Ich habe es getan. Hier ist der Code für das erwartete Ergebnis. Unten ist der Code, aus dem ich Date auswähle und über SharedPreferences an NextActivity übergebe.

calendarView.setOnDateClickListener(new CalendarView.OnDateClickListener() { 
     @Override 
     public void onDateClick(@NonNull Date selectedDate) { 
      SimpleDateFormat df_new = new SimpleDateFormat("dd-MM-yyyy", Locale.getDefault()); 
      String newselectedDate=df_new.format(selectedDate); 
      Toast.makeText(getApplicationContext(),"You selected" +newselectedDate +" : Date",Toast.LENGTH_SHORT).show(); 
      Log.v("You Selected Date is :",newselectedDate); 

      // SharedPreferences for sharing the Date with Next Activity, Added by Tara 
      SimpleDateFormat s_day=new SimpleDateFormat("dd",Locale.getDefault()); int sdd= Integer.parseInt(s_day.format(selectedDate)); 
      SimpleDateFormat s_month=new SimpleDateFormat("MM",Locale.getDefault());int smm= Integer.parseInt(s_month.format(selectedDate)); 
      SimpleDateFormat s_year=new SimpleDateFormat("yyyy",Locale.getDefault());int syy= Integer.parseInt(s_year.format(selectedDate)); 
      SharedPreferences spf= getSharedPreferences("myprfs", Context.MODE_PRIVATE); 
      SharedPreferences.Editor spe = spf.edit(); 
      // spe.putString("sdate", String.valueOf(selectedDate)); 
      spe.putInt("sday", sdd); 
      spe.putInt("smonth", smm); 
      spe.putInt("syear", syy); 
      Log.d("Day is", String.valueOf(sdd)); 
      Log.d("Month is", String.valueOf(smm)); 
      Log.d("Year is", String.valueOf(syy)); 

      spe.commit(); 

      Intent i_available_slot=new Intent(MainActivity.this, ChangeDate.class); 
      startActivity(i_available_slot); 
     } 
    }); 

Hier ist die nächste Aktivität Code, in dem das Datum der letzten Aktivität bekommen SharedPreferences

SharedPreferences spf=getSharedPreferences("myprfs", Context.MODE_PRIVATE); 
    final int id1 = spf.getInt("sday", 0); 
    final int id2 = spf.getInt("smonth", 0); 
    final int id3 = spf.getInt("syear", 0); 

    sdate= id1+"-"+id2+"-"+id3; 
    date.setText(sdate); 
    Log.v("Spf Day :", String.valueOf(id1)); 
    Log.v("Spf Month :", String.valueOf(id2)); 
    Log.v("Spf Year :", String.valueOf(id3)); 

    int month_index=id2-1; // Java Maintains Index Number from 0 so reduce the month to 1 to get the selected month 

    final Calendar c=Calendar.getInstance(); 
    System.out.println("Current time => " + c.getTime()); 
    // c.set(id1, id2, id3); 
    c.set(Calendar.YEAR,id3); 
    c.set(Calendar.MONTH,month_index); 
    c.set(Calendar.DAY_OF_MONTH,id1); 

    final SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy"); 
    formattedDate = df.format(c.getTime()); 
    Log.v("FormatedDate:",formattedDate); 

    prev.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 

      c.add(Calendar.DATE, -1); 
      formattedDate = df.format(c.getTime()); 

      Log.v("PREVIOUS DATE : ", formattedDate); 
      date.setText(formattedDate); 

     } 
    }); 
    next.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      c.add(Calendar.DATE, 1); 
      formattedDate = df.format(c.getTime()); 

      Log.v("PREVIOUS DATE : ", formattedDate); 
      date.setText(formattedDate); 

     } 
    }); 
mit
Verwandte Themen