0

I have two activities:

  • ListViewActivity
  • AddActivity

and one object class :

  • todoObj

I have a list view in my ListViewActivity activity and when I click add button this will initiate AddActivity . In AddActivity when user enters Title, choose date, time and the category I want AddActivity to create a todo object and pass this back to the ListViewActivity.

Sorry I could not share the code itself, it always gives this error all the time so I uploaded on github please check it out.

Sorry again. Many thanks.

Muhammed Refaat
  • 8,914
  • 14
  • 83
  • 118
Recomer
  • 178
  • 2
  • 12
  • "it always gives this error" - which error? – Egor Oct 21 '15 at 10:45
  • When I log it I can't see any data passed to my ListViewActivity, I can't even see the Log in my ListViewActivity 's onActivityResult. I want to know how can I get the data and pass it to another activity and crate an object, add this object to my Arraylist and show it on the ListView – Recomer Oct 21 '15 at 10:56

1 Answers1

2

All you have to fix onActivityResult method in ListViewActivity:

  1. Because of you setting result code ADD_REQUEST_CODE in AddActivity "setResult(ListViewActivity.ADD_REQUEST_CODE, intent);", you should use "if (resultCode == ADD_REQUEST_CODE) {" in ListViewActivity not RESULT_OK.

  2. You should receive intent from onActivityResult not ListViewActivity's intent. getIntent() gives ListViewActivity's intent. So use data variable:

    onActivityResult(int requestCode, int resultCode, Intent data)

Final code:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == ADD_REQUEST_CODE) {
        if (resultCode == ADD_REQUEST_CODE) {
            Log.i("ListViewActivity", "Returned onActivityResult");
            TodoObj todoObj = (TodoObj) data.getParcelableExtra("EXTRA_TODO");
            Toast.makeText(ListViewActivity.this, "" + todoObj.getmYear(),
                    Toast.LENGTH_SHORT).show();
        }
    }
}
Community
  • 1
  • 1
farukcankaya
  • 544
  • 7
  • 11
  • 1
    Many thanks for your answer, I've edited it and now it works! – Recomer Oct 22 '15 at 04:54
  • ... and I highly recommend that you use [John Ericksen](http://stackoverflow.com/users/654187/john-ericksen)'s library to make parcelable object: [github.com/johncarl81/parceler](http://github.com/johncarl81/parceler). – farukcankaya Oct 22 '15 at 09:10
  • I'm gonna check it ASAP! :) – Recomer Oct 22 '15 at 09:15