Chris G
Chris G

Reputation: 469

Passing data back and forth between activities

I have a MainActivity which has a ListView, when I click the ListView it opens a new activity for that item.

I want to be able to change the information in that item, then when I click back it changes the ListView.

Here's some of the code I have:

MainActivity:

String[] people;

private ListView mListView;

public static ArrayAdapter<String> adapter;

In onCreate(){

    people = new String[] {"", "", "", "", "", "", "", ""};

    mListView = (ListView) findViewById(R.id.personListView);
    adapter = (new ArrayAdapter<String>(this, R.layout.list_item, people);

    mListView.setAdapter(adapter);

    mListView.setOnItemClickListener(new OnItemClickListener(){

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            // Current item
            String person = ((TextView) view).getText().toString();


            // Launch new activity based on item
            Intent intent =  new Intent(getApplicationContext(), SinglePerson.class);
//              
            intent.putExtra("person", person);
            //intent.putExtra("peopleList", people);
            intent.putExtra("position", position);
            startActivityForResult(intent, 1);
//              
        }

    });

I have this in the class which I thought would get the information from the other activity back but nothing happens:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // Check which request we're responding to
    if (requestCode == 1) {
        // Make sure the request was successful
        if(resultCode == RESULT_OK){
            int listPos = data.getIntExtra("listPosition", 1);
            //edit listview value at position
            people[listPos] = data.getStringExtra("edittextvalue");
            adapter.notifyDataSetChanged();
        }
    }
}

In the other activity class:

public class SinglePerson extends Activity{

String[] people;
int position;
Intent intent;

@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    this.setContentView(R.layout.single_person_item_view);

    EditText txtPerson = (EditText) findViewById(R.id.person_name);

    intent = getIntent();

    String person = intent.getStringExtra("person");
    //people = intent.getStringArrayExtra("peopleList");
    position = intent.getIntExtra("position", 0);
    txtPerson.setText(person);

}

private TextWatcher peopleNumberListener = new TextWatcher(){

    @Override
    public void afterTextChanged(Editable s) {
        // TODO Auto-generated method stub

    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count,
            int after) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before,
            int count) {

        //people[position] = s.toString();
        //BillSplit.adapter.notifyDataSetChanged();


        intent.putExtra("edittextvalue",s.toString());
        intent.putExtra("listPosition", position);
        setResult(RESULT_OK, intent);        
        //finish();

    }

};

Upvotes: 2

Views: 1154

Answers (2)

dhiku
dhiku

Reputation: 1858

I would use Singleton if you have more than two activities. If its just two then probably using intent.put. Thanks,

Upvotes: 0

anddev84
anddev84

Reputation: 1483

As per my comment above, if you are pressing the back key then you're not properly finishing the Activity. What you want to do is when you're ready to end the Activity, either in a Button or some other action, then do the following (which it looks like you had already mostly figured out)

...
// you can create a new Intent for the result
Intent newIntent = new Intent();
newintent.putExtra("edittextvalue",s.toString());
newintent.putExtra("listPosition", position);
setResult(RESULT_OK, newintent);        
finish();
...

EDIT: In response to those who are posting to override onBackPressed(), this will allow you to intercept the back key when you press it within your Activity and decide how you want to handle it. However, please note the implication of doing this: if this is for the general public, most users will expect the back key to take you some form of "back", but this is not the same as completion or progressing through the normal flow of your app (which you are looking to do by making a selection and then continue where you left off). So while this may accomplish the desired behavior, it's debatable whether this is the correct solution for you.

Upvotes: 2

Related Questions