How to Pass Spinner Objects to other activity in A

2019-05-19 22:14发布

I tried various methods fro internet to pass spinner selected item to other class and display in text view

Below is my code. Whenever I open my app,It crashes. Your suggestions are greatly appreciated. Thanks in Advance.

This is my first class BuddyActivity

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


   Button buttonSubmit = (Button) findViewById(R.id.buttonGo);
    buttonSubmit.setOnClickListener(new OnClickListener() {
      public void onClick(View v) {
       Spinner transportSpinner = (Spinner) findViewById(R.id.spinnerSection);
          Intent i = new Intent(BuddyActivity.this.getApplicationContext(),search_project.class);
          i.putExtra("transportSpinnerValue", transportSpinner.getSelectedItem().toString());
          BuddyActivity.this.startActivity(i);

      }
    });
}  
 ;

This is my second class:

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

    Bundle extras = getIntent().getExtras();
    String transportItemChosen = extras.getString("transportSpinnerValue");

}

3条回答
走好不送
2楼-- · 2019-05-19 22:52

Class.1.java

but.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
         Spinner transportSpinner = (Spinner) findViewById(R.id.spinnerSection);
         Intent i = new Intent(BuddyActivity.this,search_project.class);
          i.putExtra("transportSpinnerValue", transportSpinner.getSelectedItem().toString());
           startActivity(i);

        }
    });

Class2.java

Intent extras = getIntent();
String transportItemChosen = extras.getStringExtra("transportSpinnerValue");
查看更多
看我几分像从前
3楼-- · 2019-05-19 23:00

Step 1: Create Layout File for Spinner

<Spinner
    android:id="@+id/email_spinner"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />

<Button
    android:id="@+id/NextActivity"
    android:text="Load"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

Step 2: Create a res/values/email.xml. This file will hold the list of values for drop down.

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="email_array">
    <item>Home</item>
    <item>Office</item>
    <item>Other</item>
</string-array>
</resources>

Step 3: Create Spinner object in program, map it to layout file. Create respective adapters. I have a fragment in my main activity. You have to customize the below code, in case you only use Activity.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_main, container, false);

    final Spinner spinner = (Spinner) rootView.findViewById(R.id.email_spinner);

    // Create an ArrayAdapter using the string array and a default spinner layout
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getActivity(),
            R.array.email_array, android.R.layout.simple_spinner_item);

    // Specify the layout to use when the list of choices appears
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    // Apply the adapter to the spinner
    spinner.setAdapter(adapter);

    Button buttonSubmit = (Button) rootView.findViewById(R.id.NextActivity);
    buttonSubmit.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Intent i = new Intent(getActivity().getApplicationContext(), Second.class);
            i.putExtra("SpinnerValue", spinner.getSelectedItem().toString());
            getActivity().startActivity(i);

        }
    });
    return rootView;
}

Step 4: Create Second Activity to receive the data and display it to TextView

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_second);
    Bundle extras = getIntent().getExtras();
    String transportItemChosen = extras.getString("SpinnerValue");
    TextView test = (TextView) findViewById(R.id.Result);
    test.setText(transportItemChosen);
}

I have tested this code and works fine!

查看更多
我欲成王,谁敢阻挡
4楼-- · 2019-05-19 23:02

Use this peice of code in your BuddyActivity:

Intent i = new Intent(BuddyActivity.this,search_project.class);
String selectedItem = transportSpinner.getSelectedItem().toString();
i.putExtra("transportSpinnerValue", selectedItem);
startActivity(i);

And use this peice of code in your second class:

Intent getItemIntent = getIntent();
String getItem = getItemIntent.getStringExtra("transportSpinnerValue");

make sure you have register your second activity in your Menifest file.

查看更多
登录 后发表回答