填充两个以上活动中使用的微调器

时间:2013-09-06 09:09:42

标签: android android-actionbar spinner android-ui

我的应用中有一个操作栏微调器和两个活动。从数据库填充的微调器需要在我的两个活动中都可见。如何在不重复代码的情况下生成它?我应该创建静态方法还是别的什么?

private NotesDbAdapter mDbHelper;
private List<String> listUniqueCat;
int selectedPos;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_joke_details);
    mDbHelper = new NotesDbAdapter(this);
    mDbHelper.open();

    //this method returns list of strings from a database
    listUniqueCat = mDbHelper.getUniqueCategories();

    // create an array adapter to popluate dropdown list
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(
        getBaseContext(),
        android.R.layout.simple_spinner_dropdown_item, listUniqueCat);

    // enable dropdown list naaavigation in action bar
    getActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);

    // defining navigation listiner
    ActionBar.OnNavigationListener navigationListener = new OnNavigationListener() {
        @Override
        public boolean onNavigationItemSelected(int itemPosition, long itemId)
        {
            selectedPos = getActionBar().getSelectedNavigationIndex();
            Toast.makeText(getBaseContext(), "selected index is "+ selectedPos ,
                                             Toast.LENGTH_LONG).show();

            return false;
        }
    };

    // setting dropdown items and item navigation listener for action bar
    getActionBar().setListNavigationCallbacks(adapter, navigationListener);
}

1 个答案:

答案 0 :(得分:1)

我认为您需要使用

等标签创建一个BaseActivity
abstract public class BaseActivity extends FragmentActivity {

}

和所有其他活动扩展了BaseActivity

小例子

BaseActivity.java

abstract public class BaseActivity extends Activity implements TabListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_first);
   ActionBar actionBar = getActionBar();
    // add tabs to actionbar
   actionBar.addTab(actionBar.newTab().setText("TAB 1")
        .setTabListener(this));
   actionBar.addTab(actionBar.newTab().setText("TAB 2")
        .setTabListener(this));

  }

}

FirstActivity.java

 public class FirstActivity extends BaseActivity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_first);
 }

 @Override
 public void onTabReselected(Tab tab, FragmentTransaction ft) {
   // TODO Auto-generated method stub

 }

 @Override
 public void onTabSelected(Tab tab, FragmentTransaction ft) {
     Toast.makeText(getApplicationContext(),
        tab.getText() + " selected in FirstActivity",
        Toast.LENGTH_SHORT).show();

   }

   @Override
   public void onTabUnselected(Tab tab, FragmentTransaction ft) {
    // TODO Auto-generated method stub

   }

 }

SecondActivity.java

 public class SecondActivity extends BaseActivity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_first);
  }

  @Override
  public void onTabReselected(Tab tab, FragmentTransaction ft) {
    // TODO Auto-generated method stub

  }

  @Override
   public void onTabSelected(Tab tab, FragmentTransaction ft) {
   Toast.makeText(getApplicationContext(),
        tab.getText() + " selected in SectondActivity",
        Toast.LENGTH_SHORT).show();

    }

   @Override
   public void onTabUnselected(Tab tab, FragmentTransaction ft) {
    // TODO Auto-generated method stub

   }

 }