0

So, I have two activities. In the first one, I have a list where I put some data and a button for going to the second activity. In the second one, I have two EditTexts and a button (save button). When I introduce some data in the EditTexts, I save the data in the first activity (list). When I choose an item of the list, I can go to the second activity and modify the content of the item. So, I display the list with the data introduced, but I don't know how save the new data after I edit again the EditTexts. I use onItemClick for choosing an item of the list. I know that I have to pass the position and the data to the second activity, I have to retrieve in the second activity and after that I have to send again to the first activity the new data changed for an item position. An idea please for doing this ?

Thank you !

Domizzi
  • 53
  • 1
  • 10
  • you could try calling "adapter. notifyDataSetChanged()" in your first (list) activity after closing the second activity. This will update your list data. Call it in "onResume" or something like that. – Prexx Oct 01 '15 at 12:14
  • I call adapter.notifyDataSetChanged when I introduce the data normally with the add button and it works ... but when I choose an item of the list with onItemClick and modify the content of a certain position I don't know how to make it ... I know that I have to send the position and the data to the second activity with an intent, but from there I don't know how to tell to the second activity that when I introduce again some data to consider the position that I want to modify ? – Domizzi Oct 01 '15 at 12:17

2 Answers2

1

Try getting the result from an activity.

To sum up the article:

static final int REQUEST_CODE = 1;  // The request code
...
private void itemClicked() {
    Intent intent = new Intent(this, SecondActivity.class);
    // Add any data that you wish to send
    intent.putExtra("DATA", "value");
    startActivityForResult(pickContactIntent, REQUEST_CODE);
}

In your second activity, receive the data you wish to modify:

String valueToChange = getIntent().getExtras().getString("DATA");

Then put it in an Edit text or whatever you want to do with it, when you are done set it as the result bundle.

// Create the result Intent
Intent intent = new Intent();
intent.putExtra("RESULT", "YourNewString");
setResult(Activity.RESULT_OK, intent);
finish();

In your first activity, override onActivityResult to get the value.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // Check which request it is that we're responding to
    if (requestCode == REQUEST_CODE) {
        if (resultCode == RESULT_OK) {
             String newString = data.getExtras().getString("RESULT");
         }
    }
}

You may also want to send through the items position in the array so that you can update it.

RobVoisey
  • 1,083
  • 15
  • 24
  • thank for your answer, but that's the problem, I don't know how to put in the EditText the value received from the first activity (the data I wish to modify). I use also startActivityForResult and setResult in the first place for realizing the list ... the problem is when I try to modify an item – Domizzi Oct 01 '15 at 12:29
  • How do you populate your list? Is there a static list with custom objects? – Prexx Oct 01 '15 at 12:30
  • @Flavius-LucianDomide not sure what the issue is then? The above code describes how to send data to and from an activity, you can pass back a new value and use it as you wish. Once you have the new value you can update the item in the list. – RobVoisey Oct 01 '15 at 12:33
  • I populate the list ... yes, is a static list with custom objects ... the problem is when I try to modify an item ... I send the position and the data that I want to modidfy, but I don't know how to put the new data in the EditText to send back to the first activity ... I hope that my explanation it's clear – Domizzi Oct 01 '15 at 12:34
  • @Flavius-LucianDomide Once you have the new data, you can simply change the item in your list and call adapter.notifyDataSetChanged() – RobVoisey Oct 01 '15 at 12:35
  • @RobVoisey This question goes to Flavius-Lucian Domide. Your answer is fine. – Prexx Oct 01 '15 at 12:38
  • @Flavius-LucianDomide Then you don't have to send data back. Just pass the position to activity 2, get the item out of the static list and show your data you want to edit in activity 2. Before closing activity 2, change/save the value for the corresponding object in the static list and refresh the list in activity 1 by calling "adapter.notifyDataSetChanged" in activity1s' onResume. Like i said 3 minutes after creating this question... :P – Prexx Oct 01 '15 at 12:39
  • and here I am, how can I save the value ? because I send the position and the data to modify to the second activity – Domizzi Oct 01 '15 at 12:43
  • @Flavius-LucianDomide right, then you modify the value and send both the value and position back to the first activity. At that point you have access to the list, the position and new value. Therefore you could just do myList.set(position, newValue); – RobVoisey Oct 01 '15 at 12:45
  • and this is the problem, I don't know how to send them back to my first activity . I retrieve them in the second activity like this : Bundle intentExtras = getIntent().getExtras(); if (intentExtras != null) { position = intentExtras.getInt(Constants.Extra.POSITION); note = intentExtras.getParcelable(Constants.Extra.NOTE); } – Domizzi Oct 06 '15 at 11:04
  • My answer explains this, I can't put it in another context because it doesn't get an simpler. Create a new intent with your new data, call `setResult(resultCode, intent)` and finish. The first activity can then catch this data in `onActivityResult` – RobVoisey Oct 06 '15 at 11:14
  • it would be more simple with an example if you have :) thank you :) because I already call setResult when I send for the first time my data ... Can I use the same intent for sending the new data or it is necessary to create a new intent ? – Domizzi Oct 06 '15 at 11:26
  • you create a new one, and i'm not going to write an example when the necessary code is posted in the answer. I hope you manage to solve your issue. – RobVoisey Oct 06 '15 at 11:28
0

Try using BroadCastReceiver for sending updated contents return to Activity-A And notify your Listview after setting the relevant content.

Bhavesh Patadiya
  • 25,740
  • 15
  • 81
  • 107