Domanda

I am trying to pass a selected date from CalendarView via an intent from activity no1 to activity no2. The activity no2 opens without any problems but the selected date is not displayed.

Here is the code for activity no1:

         String syear = Integer.toString(year);
         String smonth = Integer.toString(month);
         String sday= Integer.toString(day);

         Intent myIntent = new Intent(CalendarActivity.this, CalendarDate.class);
         Bundle extras = new Bundle();
         myIntent.putExtra(syear, "currentyear");
         myIntent.putExtra(smonth,"currentmonth");
         myIntent.putExtra(sday,"currentday");
         myIntent.putExtras(extras);
         CalendarActivity.this.startActivity(myIntent);

and this is the code for activity no2:

Bundle extras = getIntent().getExtras();

    String dyear = extras.getString("currentyear");
    String dmonth = extras.getString("currentmonth");
    String dday = extras.getString("currentday");


    TextView textView = new TextView(this);
    textView.setTextSize(40);
    textView.setText(dday);
    textView.setText(dmonth);
    textView.setText(dyear);

    setContentView(textView);

What am I doing wrong ?

È stato utile?

Soluzione

It should be

    myIntent.putExtra("currentyear",syear);
    myIntent.putExtra("currentmonth",smonth);
    myIntent.putExtra("currentday",sday);

Also use append

    textView.append(dday);
    textView.append(dmonth);
    textView.append(dyear);

Altri suggerimenti

In activity no 1 have like this.

     myIntent.putExtra("currentyear", syear);
     myIntent.putExtra("currentmonth",smonth);
     myIntent.putExtra("currentday",sday);
     myIntent.putExtras(extras);

And in the activty no 2 Always have the null check placed when reading from the extras.

have like this.

Bundle extras = getIntent().getExtras();
if(extras!=null){
String dyear = extras.getString("currentyear");
String dmonth = extras.getString("currentmonth");
String dday = extras.getString("currentday");
}


TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(dday);
textView.setText(dmonth);
textView.setText(dyear);

setContentView(textView);
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top