user218649
user218649

Reputation: 73

Trouble with intent

I am trying to make a date to day converter, which will show the day of the week in another activity

Here i put in the date

private static final int SHOW_SUBACTIVITY = 1;

public void startSubActivity() {
    Intent intent = new Intent(this, SubActivity.class); 
    intent.putExtra("date", mEditText.getText().toString());
    startActivityForResult (intent, SHOW_SUBACTIVITY);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mEditText = (ETView)findViewById(R.id.editText1);
    Button btn = (Button) findViewById(R.id.button1);



    btn.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            startSubActivity();
        }
    }
    );
}

Here it shows the day of the week

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sub);

    mTextView = (TView)findViewById(R.id.textView);

    Date date = new Date();
    Calendar c = Calendar.getInstance();
    try{
        String str_date= getIntent().getExtras().getString("DATE");
        SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yyyy");
        date = (Date)formatter.parse(str_date);
        c.setTime(date);
        Log.d(TAG, date.toString());
        int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);

        String s3 = c.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.getDefault());
        mTextView.setText(s3);
        }catch (Exception e){
            mTextView.setText("Wrong input");
        }
}
}

The problem is, that the program always shows "Wrong input".

Upvotes: 0

Views: 54

Answers (1)

Bossie
Bossie

Reputation: 3612

The extra you put in is called "date", while the one you get out is called "DATE". I'm willing to bet that parse() throws a ParseException.

Make sure that you name your extras properly.

Also, a slightly better way would be to do the parsing in your first activity, and put the Date into your extras as a Serializable with putSerializable() instead of as a String.

Upvotes: 1

Related Questions