I'm trying to select an item in Spinner and that item will open an activity when I click the send button. For example, Activity 1 and Activity 2. In my spinner, I have Item 1 and Item 2. When I choose Item 1, I want Activity 1 to be open. I've tried some code but it doesn't working. Here's my code.
Spinner in activity_main.xml
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/spinner"
android:layout_alignBottom="@+id/button"
android:layout_toRightOf="@+id/button"
android:layout_toEndOf="@+id/button"
android:entries="@array/punpColleges"
android:spinnerMode="dropdown" />
In my MainActivity.java
public class MainActivity extends ActionBarActivity {
Spinner spin = (Spinner) findViewById(R.id.spinner);
private static Button button_send;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
OnClickButtonListener();
}
public void OnClickButtonListener() {
button_send = (Button) findViewById(R.id.button);
button_send.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
if (spin.getSelectedItem().toString().equals("CCS")) {
startActivity(new Intent(MainActivity.this, ListActivity.class));
} else {
startActivity(new Intent(MainActivity.this, SecondTesting.class));
}
}
}
);
}
Strings are located in strings.xml
<string-array name="punpColleges">
<item>CCS</item>
<item>CBE</item>
</string-array>
I also added this in AndroidManifest.xml
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".ListActivity"
android:label="@string/title_activity_list" >
<intent-filter>
<action android:name="android.intent.action.ListActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
Thanks in advance for the help!
On button click try this to call your Activity
try {
myClass = Class.forName("com.example.yourPackageName." + classComplete); //classcomplete as your file name, take it from spinner selected object
Intent myIntent = new Intent(MainActivity.this, myClass);
startActivity(myIntent);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
You can't initialize your spinner at class level. It has to be done after calling setContentView
. findViewById
can't be used unless you have set the layout to the activity.
Define the spinner like this
Spinner spin;
Add
spin = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(
this,
android.R.layout.simple_spinner_dropdown_item,
getResources().getStringArray(R.array.punpColleges));
spin.setAdapter(spinnerAdapter);
inside onCreate
after calling setContentView
Define Spinner and Variable to store position of spinner
int x=0;
Spinner spin=(Spinner) findViewById(R.id.spinner);
ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_dropdown_item,
list);
spin.setAdapter(spinnerAdapter);
spin.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent,
View view, int position, long id) {
x=position;
});
}
Now compare the postion on Button click
if(x==0)
Intent myIntent = new Intent(MainActivity.this, myClass1);
startActivity(myIntent);
else
Intent myIntent = new Intent(MainActivity.this, myClass2);
startActivity(myIntent);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.