Domizzi
Domizzi

Reputation: 63

Save again the data in List Android

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 !

Upvotes: 0

Views: 91

Answers (2)

RobVoisey
RobVoisey

Reputation: 1083

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.

Upvotes: 1

Bhavesh Patadiya
Bhavesh Patadiya

Reputation: 25830

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

Upvotes: 0

Related Questions