0

I'm trying to add data to a recycler view adapter but the getStringExtra is always null. I maybe making a crucial flaw in what I am doing as I'm not the most experienced programmer.

What is being called first:

public void addItem(View view) {
        Intent otherOne = new Intent(this, Main2Activity.class);
        startActivity(otherOne);

        Intent intent = getIntent();
        String subjectString = intent.getStringExtra("subject");
        String detailsString = intent.getStringExtra("details");
        String dateString =   intent.getStringExtra("date");



        Data dataToAdd = new Data(
                subjectString,
                detailsString,
                dateString);
        mData.add(dataToAdd);
        Log.d(TAG, dateString + "might have workedlol");
        // Update adapter.
        mAdapter.addItem(mData.size()-1, dataToAdd);
    }

What is called while in MainActivity2:

public void setData(View view){


        inputSubject = (EditText) (findViewById(R.id.editTextSubject));
        String stringSubject = inputSubject.getText().toString();

        inputDetails = (EditText) findViewById(R.id.editTextDetails);
        String stringDetails = inputDetails.getText().toString();

        inputDate = (EditText) findViewById(R.id.editTextDate);
        String stringDate = inputDate.getText().toString();

        Intent intent = new Intent(this, PlannerActivity.class);
        intent.putExtra("subject", stringSubject.toString());
        Log.d(TAG, stringDate + "might have worked");
        intent.putExtra("details", stringDetails.toString());
        intent.putExtra("subject", stringDate.toString());
        startActivity(intent);
    }
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • You don't need the stringX.`toString()`, when you already did the getText().toString(), but what's more interesting is to log stringDetails, stringDate as well in setData, and let us know if they have real value there or already empty there? – Gavriel Feb 02 '16 at 20:53
  • This definitely looks flawed. It looks like you are starting `MainActivity2` and expecting results from it to be immediately returned which is not the case. If you want to return results from an `Activity` try using `startActivityForResult` instead as shown [here](http://developer.android.com/training/basics/intents/result.html). – George Mulligan Feb 02 '16 at 20:57
  • @Code-Apprentice Doubtful given the posted code is trying to use `get` methods on the `Intent`. If that is the case then the first duplicate will cover that anyways. – George Mulligan Feb 02 '16 at 21:05
  • @GeorgeMulligan I deleted my previous comment now that I see that it was incorrect. – Code-Apprentice Feb 02 '16 at 21:06

1 Answers1

2

You have two errors:

  1. You are incorrectly trying to handle data returned from an activity. There are several tutorials about how to do this. You will need to use startActivityForResult() and override the onActivityResult() callback. See Getting a Result from an Activity for details.

  2. You are setting the "subject" twice, once with the stringSubject and then again with stringDate.

As an aside, the word "string" in your variable names is redundant and unnecessary typing. I suggest that you use simpler names like date and subject. Also, there is no reason to call toString() on a String object since it is already the correct type.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268