Reputation: 15
I have a spinner with items and I want when I click one of them items, it opens another activity
Spinner in layout of MainActivity
<Spinner
android:id="@+id/spinner1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignRight="@+id/imageView1"
android:entries="@array/spinner1"
/>
Spinner in strings.xml
<string-array name="spinner1">
<item>NEWS</item>
<item>RESULTS</item>
<item>CLASIFICATION</item>
</string-array>
I want to link "RESULTS" with Results.java, and "CLASIFICATION" with Clasification.java; What code should I use and where? Thanks
Upvotes: 0
Views: 2213
Reputation: 11131
try this...
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
TextView textView = (TextView) view;
String text = textView.getText().toString().toLowerCase(Locale.getDefault());
text = Character.toUpperCase(text.charAt(0))+""+text.substring(1);
try {
Class<?> cls = Class.forName("yourPackageName."+text);
Intent intent = new Intent(SecondActivity.this, cls);
startActivity(intent);
} catch (Exception e) {
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
Upvotes: 0
Reputation: 6402
You can add a OnItemSelectedListener
to your Spinners and from there you can get the data that you have selected.
You can then navigate to each activity according to the selected data.
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View arg1,
int position, long arg3) {
String data = spinnerDataSource.get(position);
if(data.equals("RESULTS")) {
//navigates to result class
}else if(data.equals("CLASIFICATION")) {
//navigates to classification class
}else {
//navigates to News class
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
Upvotes: 0
Reputation: 23638
Try out as below:
spin.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
System.err.println("**************" + arg2);
switch (arg2) {
case 0:
Intent i = new Intent();
i.setClass(CurrentActivity.this, NEWS.class);
startActivity(i);
break;
case 1:
Intent ir = new Intent();
ir.setClass(CurrentActivity.this, Results.class);
startActivity(ir);
break;
case 2:
Intent ic = new Intent();
ic.setClass(CurrentActivity.this, Clasification.class);
startActivity(ic);
break;
}}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
Upvotes: 0
Reputation: 5295
You can use the code below :-
Spinner s = (Spinner)findViewbyId(R.id.spinner1);
s.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View v,
int pos, long arg3) {
switch (pos) {
case 0:
Intent intent = new Intent(MainActivity.this,News.class);
startActivity(intent);
break;
case 1:
//similar code here like above
break;
case 2:
break;
default:
break;
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
Upvotes: 0
Reputation: 3322
youspinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View arg1,
int position, long arg3) {
if(position == requiredposion)
{
Intent i = new Intent();
i.setClass(CurrentActivity.this, NextActivity.class);
startActivity(i);
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
Upvotes: 1