Reputation: 121
I have spinner which including many items and want when I click on one item from those items it open another activity
here is the spinner in layout
<Spinner
android:id="@+id/spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:prompt="@string/title"
android:entries="@array/items"
/>
here is the items in string
<string name="title">select</string>
<string-array name="items">
<item>open activity one</item>
<item>open activity two</item>
</string-array>
here is the code that i want to make it able to open another activity when i click on the items
Spinner Spinner = (Spinner) findViewById(R.id.spinner);
Spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
}
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
Upvotes: 2
Views: 18037
Reputation: 1
String selection ;
acTV1.setAdapter(arrayAdapter);
acTV1.setCursorVisible(false);
acTV1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
acTV1.showDropDown();
String selection = (String) parent.getItemAtPosition(position);
Toast.makeText(getApplicationContext(), selection, Toast.LENGTH_SHORT);
if(selection.equals("Delete"))
{
intent = new Intent(ImageAttachmentActivity.this, DeleteEmployeeActivity.class);
startActivity(intent);
}
});
Upvotes: 0
Reputation: 11
public class Main2Activity extends Activity {
Toolbar mytoolbar;
Spinner mySpinner;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
mytoolbar=(Toolbar) findViewById(R.id.toolbar);
mySpinner=(Spinner) findViewById(R.id.spinner);
ArrayAdapter<String> myAdaptor=new ArrayAdapter<String>(Main2Activity.this,
R.layout.custom_spinner_item,
getResources().getStringArray(R.array.names));
myAdaptor.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mySpinner.setAdapter(myAdaptor);
mySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
}
@Override
public void onItemSelected(AdapterView<?> arg0, View view,
int position, long row_id)
{
final Intent intent;
switch(position)
{
case 1:
intent = new Intent(Main2Activity.this, MainhomeActivity.class);
startActivity(intent);
break;
case 2:
intent = new Intent(Main2Activity.this, DateActivity.class);
startActivity(intent);
break;
// and so on // .....
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
}
Upvotes: 1
Reputation: 1037
try this:
Spinner Spinner = (Spinner) findViewById(R.id.spinner);
Spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
}
@Override
public void onItemSelected(AdapterView<?> arg0, View view,
int position, long row_id) {
final Intent intent;
switch(position){
case 1:
intent = new Intent(CurrentActivity.this, TargetActivity1.class);
break;
case 2:
intent = new Intent(CurrentActivity.this, TargetActivity2.class);
break;
// and so on
// .....
}
startActivity(intent);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
Upvotes: 4
Reputation: 330
you already have the onItemSelected
method. So just put a switch-case statement in there with arg2 as argument. arg2 is the position of the item. so just put your intent to open another activity in the case you need. cheers
Upvotes: 0