切换片段时为什么我的按钮文字没有显示?

时间:2014-10-07 13:32:22

标签: android android-fragments

我正在为我的实习制作一个Android应用程序,所以我还不是那么好。我有五个片段,都显示有关公司的不同内容。第一个片段立即加载到MainActivity的onCreate()。在第一个片段中,我得到了6个按钮,这些按钮将指向其他片段(除了将使用“调用”功能的片段)。按钮的标题将由其网站公司的xml Feed填充(这一切都正常)。

我想要的方式:

当我切换到另一个片段(片段2的服务)并切换回片段1(主片段)时,它将获取xml源的存储值并重新填充按钮的标题。

发生了什么,是我的问题: 当我从家中切换到另一个片段并返回到主片段时,我的主片段按钮标题将不会被存储的数据填充。

代码发生的地方:

public void selectItem(int position) {


    listView.setItemChecked(position, true);
    position_array_items = position;        
    simple_array = fragment_items.toArray(new String[fragment_items.size()]);

    if (position == 4) {
       // this is the call function and won't show a title for the actionbar
    }else{
        setTitle(simple_array[position]);
    }

    Fragment_a fragment_a = new Fragment_a();
    Fragment_b fragment_b = new Fragment_b();
    Fragment_c fragment_c = new Fragment_c();
    Fragment_d fragment_d = new Fragment_d();
    Fragment_e fragment_e = new Fragment_e();

    switch (position) {

    case 0:                 
        if (!fragment_a.isVisible()) {              
            FragmentTransaction transaction = manager.beginTransaction();
            transaction.replace(R.id.mainContent, fragment_a, "fragmentA");
            transaction.addToBackStack("fragmentA");

            transaction.commit();           
        }

        Button button_main_menu_one = config.button_main_menu_one;
        Button button_main_menu_two = config.button_main_menu_two;
        Button button_main_menu_three = config.button_main_menu_three;
        Button button_main_menu_four = config.button_main_menu_four;
        Button button_main_menu_five = config.button_main_menu_five;
        Button button_main_menu_six = config.button_main_menu_six;

        button_main_menu_one.setText(config.fragment_title_1);// Home
        button_main_menu_two.setText(config.fragment_title_2);// Services
        button_main_menu_three.setText(config.fragment_title_3);// About us
        button_main_menu_four.setText(config.fragment_title_4);// Opening times
        button_main_menu_five.setText(config.fragment_title_5);// Call
        button_main_menu_six.setText(config.fragment_title_6);// Contact


        drawerLayout.closeDrawers();
        break;

    case 1:                 
        if (!fragment_b.isVisible()) {              
            FragmentTransaction transaction = manager.beginTransaction();
            transaction.replace(R.id.mainContent, fragment_b, "fragmentB");
            transaction.addToBackStack("fragmentB");
            transaction.commit();
        }
            drawerLayout.closeDrawers();
        break;
    case 2:     
        if (!fragment_c.isVisible()) {              
            FragmentTransaction transaction = manager.beginTransaction();
            transaction.replace(R.id.mainContent, fragment_c, "fragmentC");
            transaction.addToBackStack("fragmentC");
            transaction.commit();
        }
            drawerLayout.closeDrawers();
        break;
    case 3:     
        if (!fragment_d.isVisible()) {              
            FragmentTransaction transaction = manager.beginTransaction();
            transaction.replace(R.id.mainContent, fragment_d, "fragmentD");
            transaction.addToBackStack("fragmentD");
            transaction.commit();
        }
            drawerLayout.closeDrawers();
        break;
    case 4:     
        String phoneNumber = "0123456789";
        Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phoneNumber));
        startActivity(intent);
        break;
    case 5:     
        if (!fragment_e.isVisible()) {              
            FragmentTransaction transaction = manager.beginTransaction();
            transaction.replace(R.id.mainContent, fragment_e, "fragmentE");
            transaction.addToBackStack("fragmentE");
            transaction.commit();
        }
            drawerLayout.closeDrawers();
        break;
    default:            
        break;
    }                       
}

public void setTitle(String title){
    getSupportActionBar().setTitle(title);      
}

1 个答案:

答案 0 :(得分:0)

我修复了我的问题,如下所示:我曾经在导航抽屉中点击项目时设置了文本,但不知怎的,它确实设置了值但没有显示它。我去了片段类并初始化那里的按钮并设置文本并且它有效。