1

我目前正在使用 ActionBarSherlock 创建我的操作栏。现在,我有一个从先前活动中的 Id 中选择的数组创建的选项卡。默认情况下,应用程序在此页面上启动,并默认选择第一个数组的第一个选项卡。我遇到的问题是当方向改变时,活动被破坏并再次创建,导致第一个数组的第一个选项卡显示。我试图让标签在创建时显示保存的标签,但我得到了有趣但错误的结果。当谈到 Android 时,我不是一个向导,我希望这是一个简单的修复。

该应用程序所做的是用户从先前的活动中选择一个类别,并根据从意图发送的 id,有时需要或不需要不同的选项卡。所以我使用一个数组来构建类别的选项卡。选择选项卡后,它会执行 JSON 查询并获取信息并将其显示在列表视图中。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(true);
    setContentView(R.layout.pt_section);

    //use array within array to make tabs
    ActionBar bar = getSupportActionBar();
    ActionBar.Tab tab =null;     
    getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);        
    String[][] section_id_name = {
            { "News", "Opinion", "Features", "Sports", "Sustainable Life"}
            ,{ "News", "Opinion", "Features", "Sports"}
            ,{ "News", "Features" }

            };

    String[][] section_id = {
            { "1", "4", "13", "41", "42"}
            ,{ "5", "7", "11", "12"}
            ,{ "8", "9" }

            };


                Intent intent = getIntent();
                //Check if sid is null
                 sid = intent.getStringExtra(PT_newspapers.INTENT_ID);  
                if (sid == null){
                    sid = "0";          
                }

                    sid_int = Integer.parseInt(sid);
                    for (int i = 0; i < section_id[sid_int].length; i++) { 
                        tab = getSupportActionBar().newTab();
                        tab.setText(section_id_name[sid_int][i]);
                        //sid = sid1id[i];
                        tab.setTag(section_id[sid_int][i]);
                        tab.setTabListener(this);
                        getSupportActionBar().addTab(tab); 
                        System.out.println(section_id[sid_int][i]);

            }

                    if (savedInstanceState != null) {

                        bar.setSelectedNavigationItem(savedInstanceState.getInt("tab", 1));
                        System.out.println("not null!"); 
                    }       




}//End onCreate method


@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putInt("tab", getSupportActionBar().getSelectedNavigationIndex());
}

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

    sectionid = tab.getTag().toString();
    System.out.println("Tab selected with id of :" + sectionid);       
    productsList = new ArrayList<HashMap<String, String>>();

    System.out.println("Now executing LoadAllProducts class");
    new LoadAllProducts().execute(); //Calls to class LoadAllProducts
    ListView lv = (ListView)findViewById(R.id.list2);
    lv.setOnItemClickListener(new OnItemClickListener() {


        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {

            // getting values from selected ListItem


            String id2 = ((TextView) view.findViewById(R.id.id_tag)).getText()
                    .toString();

            // Starting new intent
            Intent in = new Intent(getApplicationContext(),PT_Article.class);
            // sending sid to next activity
           in.putExtra(INTENT_ID, id2);

            // starting new activity and expecting some response back
            //startActivity(in);

            startActivityForResult(in, 100);
            overridePendingTransition(0,0);
            //mError.setText("Error, No stories loaded");
        }

    }); 


} //End onTabSelected 

方向更改后,该应用程序可以在选项卡更改时显示正确的故事,但是当它最初切换时,它将保存当前选项卡,但显示错误的数据。

我整天都在试图弄清楚这一点,但没有运气,任何见解都将不胜感激。

4

0 回答 0