[英]Use spinner in external class because needed in all other activities
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: 我的问题是如何将微调器放在外部java类中,并在所有其他活动中实现它(作为菜单工作),这是我的微调器代码:
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? 应该将其保存在例如menu.java中,并且应该在每次活动中调用,我该如何正确执行? 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.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. 现在,代替getApplicationContext()可以通过传递要使用Spinner属性的类的对象来调用它。
Menu object = new Menu(object_of_current_class); object.ShowSpinner();
Try creating your own Spinner class: 尝试创建自己的Spinner类:
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 如果您想获得更多详细信息,请访问http://developer.android.com/training/custom-views/index.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.