在外部类中使用微调器,因为在所有其他活动中都需要

时间:2014-09-02 12:39:56

标签: java android class spinner external

我的问题是如何将我的微调器放在外部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  
            }
        });

它应该保存在例如menu.java中,应该在每个活动中调用,我该怎么做呢?提前谢谢你。

3 个答案:

答案 0 :(得分:1)

如果您在所有活动中都有相同的菜单,最好的方法是创建一个扩展Activity的超类,让所有其他活动扩展此活动。

public class BaseActivity extends Activity { // menu code  }

public class StartingPoint extends BaseActivity { //... } 

答案 1 :(得分:0)

  1. 为menu.java创建构造函数

          menu(Context mcontext){
              this.mcontext = mcontext;
            }
    
  2. 现在代替getApplicationContext()通过传递要使用Spinner属性的类对象来调用它。

           Menu object = new Menu(object_of_current_class);
            object.ShowSpinner();
    

答案 2 :(得分:0)

尝试创建自己的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);   
      .
      .
      .
   }
}

然后将其添加到您的布局中

<com.yourpackage.MenuSpinner
   android:id="comboCasino"
   .
   .
   . />

如果您想了解更多详细信息,请访问http://developer.android.com/training/custom-views/index.html