Rob B
Rob B

Reputation: 23

Android: Pass data to new Activity

I'm new to android programming, and I'm pretty sure this is what I need. I've got a page that loads on start that has a spinner, some edit text boxes, and two buttons. One button clears out the boxes and resets the spinner. The other button should load a new activity (I guess) and I need what was in the EditTexts and the spinner to be loaded by that.

Right now I have it making a toast so I could test that everything was being grabbed properly. Here is my activity I have now:

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

public class directoryApp extends Activity {

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.searchscreen);

        //Declaring these so I can use them when the button is clicked.
        final Spinner depts = (Spinner) findViewById(R.id.dept);
        final ArrayAdapter<CharSequence> deptsAdapter = new ArrayAdapter<CharSequence>(this, android.R.layout.simple_spinner_item);
        deptsAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        final EditText fname = (EditText) findViewById(R.id.fname);
        final EditText lname = (EditText) findViewById(R.id.lname);
        final EditText aim = (EditText) findViewById(R.id.aim);
        final EditText phone = (EditText) findViewById(R.id.phone);
        depts.setAdapter(deptsAdapter);
        for(int i = 0 ; i < fillSpinner().length ; i++){
            deptsAdapter.add(fillSpinner()[i]);
        }


        //Search Button on click, makes a "toast" message with grabbed web string and whatever was entered into the fields.
        findViewById(R.id.search).setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                //Here is where the search button does it's thing.
                Toast.makeText(BNYDirectory.this, fname.getText() + " " + lname.getText() + " " + aim.getText() + " " + phone.getText() + " " + depts.getSelectedItem() + " " + deptIDs()[(int)depts.getSelectedItemId()], Toast.LENGTH_SHORT).show();
            }
        });
    }
}

I need to pass fname.getText(), lname.getText(), aim.getText(), phone.getText(), and deptIDs()[(int)depts.getSelectedItemId()] (all of which I am getting as strings).

What's the best way to go to the next page (which I will be using these to get results and display)? It's also possible that on click on the search I can have it grab the results from the web and put it into a string array and only pass that, but I'd rather not do that.

Any example I could use?

Upvotes: 2

Views: 2816

Answers (1)

Ted Hopp
Ted Hopp

Reputation: 234797

You create an Intent specifying the class name for the second activity. Then you add extra data to the intent, with each piece of data associated with a unique string that serves as a key for finding the data. The new activity can then retrieve the data using the same keys. See the section Opening a New Screen in the Common Tasks section of the resources docs for sample code.

Upvotes: 7

Related Questions