我目前正在使用ActionBarSherlock来创建我的操作栏。现在,我有一个从数组中创建的选项卡,该数组是从上一个活动中的Id中选择的。默认情况下,应用程序在此页面上启动,默认为选择第一个数组的第一个选项卡。我遇到的问题是当方向改变时,活动被销毁并再次创建,导致第一个数组的第一个选项卡显示。我试图让选项卡显示保存的选项卡,当它是预制件onCreate时,但我感兴趣,但结果不对。对于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
一旦方向发生变化,应用程序就可以在标签更改上显示正确的故事,但是当它最初切换时,它将保存当前标签,但显示错误的数据。
我一直试图弄清楚这一天,但没有运气,任何见解都会非常感激。