0

我有一个问题,在我的应用程序中实现导航。我想用Spinner之类的菜单,在那里你可以选择你感兴趣的item并获得对应的数据。然后这些数据将被设置为ActionBar选项卡。导航在应用程序:微调+ ActionBar标签

App

我实现了两个微调和标签,但有一个问题发生。 Android,不允许在OnItemSelectedListener中使用可变变量,而我需要它。

final ActionBar.Tab tabToday= actionBar.newTab().setText("Today"); 
final ActionBar.Tab tabSoon = actionBar.newTab().setText("Soon"); 

.............................. 

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){ 

      @Override 
      public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) { 
       switch(arg2){ 
       case 0: 
        tabToday.setTabListener(new CustomTabListener(MainActivity.this,"item_1", "url")); 
        tabSoon.setTabListener(new CustomTabListener(MainActivity.this,"item_1", "url_2")); 

        actionBar.addTab(tabToday); 
        actionBar.addTab(tabSoon);    
        break; 

       case 1: 
        tabToday.setTabListener(new CustomTabListener(MainActivity.this,"item_2", "url_3")); 
        tabSoon.setTabListener(new CustomTabListener(MainActivity.this,"item_2", "url_4")); 

        break; 

       case 2: 
        tabToday.setTabListener(new CustomTabListener(MainActivity.this,"item_3", "url_5")); 
        tabSoon.setTabListener(new CustomTabListener(MainActivity.this,"item_3", "url_6")); 
        break; 
       } 
     } 

的问题是,我不能使用不final变量,由于这种情况的发生:

enter image description here

有没有什么办法来解决这个问题,但仍然是相同的导航? 感谢您的阅读,任何帮助将不胜感激。 :)

编辑: 问题不是出在最后的变量,但问题依然存在:((

+1

你不能让tabToday和tabSoon的最终副本,并使用监听器里的变量,像这样: 最终ActionBar.Tab tabTodayCopy = tabToday。那么今天就不必是最后的决赛了。 – 2013-03-21 18:42:00

+0

tabToday不会是最终的,但这些最终副本将转到actionBar。最后一切都会是一样的:( – 2013-03-21 18:53:09

+0

嘿,你能给一个提示你怎么能够添加标签和微调都到你的应用程序?我面临的问题 – Harshit 2014-02-05 07:17:58

回答

1

我不知道为什么最后将导致该问题,但你能避免使用该关键字通过不通过使您的活动创建一个匿名类实现AdapterView.OnItemSelectedListener

,并呼吁

spinner.setOnItemSelectedListener(this); 
+0

描述这样的类'公共类MainActivity扩展活动实现AdapterView。 OnItemSelectedListener' 然后实现里面的OnItemSelectedListener? 如果不是最终变量导致这个,它会是什么? – 2013-03-21 18:58:51

+0

你说得对,问题不在最终变量:(但现在我不知道为什么会发生这种情况 – 2013-03-21 19:07:02

+0

@AndrewRahimov:你好,你有没有找到实现目标的替代方案,我也在寻找相同的方法 – inderbagga 2013-09-18 10:50:29

0

已经经历了这个问题,其中列表将始终将相同的数据填充到每个交互。在我的情况下,问题是,当你升级的片段:

 if(mFragment==null){ 
       mFragment = Fragment.instantiate(mActivity, mClass.getName()); 
    /*correct:*/ ft.replace(android.R.id.content,mFragment, mTag); 
       ft.add 
     }else{ 
/*correct:*/ ft.attach(mFragment); 
     } /*notUse*/ ft.add(mFragment);