My question is how can i put my spinner in an external java class and implement it in all other activities (works as a menu), here is my spinner code:
final Spinner spinner = (Spinner) findViewById(R.id.comboCasino);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.comboCasino, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setSelection(0, false);
// this will be called when you select any item in this spinner
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View arg1, int position, long arg3) {
// get the text at that position
switch(position) {
case 0: {
Intent NewPost = new Intent(getApplicationContext(), StartingPoint.class);
startActivity(NewPost);
break; }
case 1: {
Intent NewPost = new Intent(getApplicationContext(), Simmering.class);
startActivity(NewPost);
break; }
case 2: {
Intent NewPost = new Intent(getApplicationContext(), LugnerCity.class);
startActivity(NewPost);
break; }
case 3: {
Intent NewPost = new Intent(getApplicationContext(), Gmunden.class);
startActivity(NewPost);
break;}
case 4: {
Intent NewPost = new Intent(getApplicationContext(), Salzburg.class);
startActivity(NewPost);
break; }
case 5: {
Intent NewPost = new Intent(getApplicationContext(), Linz.class);
startActivity(NewPost);
break; }
case 6: {
Intent NewPost = new Intent(getApplicationContext(), Saalbach.class);
startActivity(NewPost);
break; }
case 7: {
Intent NewPost = new Intent(getApplicationContext(), Innsbruck.class);
startActivity(NewPost);
break;}
case 8: {
Intent NewPost = new Intent(getApplicationContext(), Reutte.class);
startActivity(NewPost);
break; }
case 9: {
Intent NewPost = new Intent(getApplicationContext(), Bregenz.class);
startActivity(NewPost);
break; }
case 10: {
Intent NewPost = new Intent(getApplicationContext(), Kufstein.class);
startActivity(NewPost);
break; }
case 11: {
Intent NewPost = new Intent(getApplicationContext(), Bratislava.class);
startActivity(NewPost);
break; }
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
it shoud be saved for example in menu.java and should be called in every activity, how do i do that correctly? thank you in advance.
If you have the same menu in all your activities, the best way to do this is by creating a superclass that extends Activity and let all your other activities extend this activity.
public class BaseActivity extends Activity { // menu code }
public class StartingPoint extends BaseActivity { //... }
Create a constructor for menu.java
menu(Context mcontext){ this.mcontext = mcontext; }
Now in place of getApplicationContext() call it by passing of object of class where you want to use Spinner property.
Menu object = new Menu(object_of_current_class); object.ShowSpinner();
Try creating your own Spinner class:
public class MenuSpinner extends Spinner {
public MenuSpinner(Context context) {
super(context);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.comboCasino, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
setAdapter(adapter);
setSelection(0, false);
.
.
.
}
}
And then add it in your layout
<com.yourpackage.MenuSpinner
android:id="comboCasino"
.
.
. />
If you want more detailled information visit http://developer.android.com/training/custom-views/index.html
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.