0

我无法将日历中的日期传递给其他活动。这不是我想要显示的当前日期,而是用户在我编码的日历上选择的日期。
以下是我的代码。

                            @Override
                public void onClick(View view)
                {
                    String date_month_year = (String)view.getTag();
                    selectedDayMonthYearButton.setText(new StringBuilder().append("Selected:").append(date_month_year));

                    Intent k = new Intent(Calendar_Event.this, Create_Events.class);
                 startActivity(k);

                    try
                    {
                        Date parsedDate = dateFormatter.parse(date_month_year);
                        Log.d(tag, "Parsed Date: " + parsedDate.toString());
                    }
                    catch(ParseException e)
                    {
                        e.printStackTrace();
                    }
                } 


                public int getCurrentDayOfMonth()
                    {
                        return currentDayOfMonth;
                    }

                private void setCurrentDayOfMonth(int currentDayOfMonth)
                    {
                        this.currentDayOfMonth = currentDayOfMonth;
                    }
                public void setCurrentWeekDay(int currentWeekDay)
                    {
                        this.currentWeekDay = currentWeekDay;
                    }
                public int getCurrentWeekDay()
                    {
                        return currentWeekDay;
                    }

        }//end GridCellAdapter

我试图将选择的 onClick 日期传递给该类链接到的下一个活动。
任何帮助将不胜感激,谢谢!

4

2 回答 2

0

用于Intent.putExtra(String date)将日期从一项活动传递到另一项活动。更改您的按钮单击代码,如 Calendar_Event.class:

在 Calendar_Event 活动中:

//Create new Intent Object, and specify class
Intent intent = new Intent();

intent.setClass(Calendar_Event.this,Create_Events.class);

intent.putExtra("passdate","Date_var_here");  

Calendar_Event.this.startActivity(intent);

在 Create_Events 活动中:

//obtain  Intent Object send  from SenderActivity
  Intent intent = this.getIntent();

  /* Obtain String from Intent  */
  if(intent !=null)
  {
     String strdata = intent.getExtras().getString("passdate");
    // DO SOMETHING HERE
  }
  else
  {
    // DO SOMETHING HERE
  }
于 2012-08-02T04:05:36.890 回答
0

考虑通过调用将日历变量转换为 longgetTimeInMillis()

/* GetDateActivity.java */

Intent intent = new Intent();

Calendar myDate = Calendar.getInstance();
myDate.set(Calendar.YEAR, 1970);
myDate.set(Calendar.MONTH, 1);
myDate.set(Calendar.DAY_OF_MONTH, 1);

intent.putExtra("myDateKey", myDate.getTimeInMillis());
setResult(RESULT_OK, intent);
// Terminate current activity
finish();

然后,内部活动称为startActivityForResult(intent),即MainActivity.java

public static final int REQUEST_DATE_CODE = 777;

...

Intent i = new Intent(MainActivity.class, GetDateActivity.class);
startActivityForResult(i, REQUEST_DATE_CODE);

...

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
        case REQUEST_DATE_CODE:
            if (resultCode == RESULT_OK && data != null) {

                Calendar myDate = Calendar.getInstance();
                myDate.setTimeInMillis(extras.getLong("myDateKey"));

                Date date = myDate.getTime();
                SimpleDateFormat simpleDateFormatter = new SimpleDateFormat("EEE, MMM d, ''yy", Locale.getDefault());
                Log.d(tag, "Parsed Date: " + simpleDateFormatter.format(date));
           }
           break;
    }
}
于 2017-12-06T19:35:53.660 回答