I'm working on an application that features all Brazilian championship games.
I want each game to appear in one round, for example:
tab1 (round1)
tab38 (round38)
For this purpose I created some tabs using Sherlock.
The games come from the Database I created in SQLite.
My question is how to display the rounds on each tab?
I already have the class that looks for the data of bd and throws them in a list and then the adapter takes care of popular a ListView that is inside the tabs.
How to put a different round in each TAB?
Here are my main codes: MainActivity.java
public class MainActivity extends SherlockFragmentActivity {
private ActionBar actionBar;
private ViewPager viewPager;
private int rodadas=38;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
actionBar= getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
viewPager = (ViewPager) findViewById(R.id.pager);
viewPager.setOnPageChangeListener(onPageChangeListener);
viewPager.setAdapter(new ViewPagerAdapter(getSupportFragmentManager(),preparaTab(1)));
addActionBarTabs();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
super.onCreateOptionsMenu(menu);
getSupportMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.item_table:
Toast.makeText(this, "Tabela", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
return true;
}
private List<Jogo> preparaTab(int rodada) {
// TODO Auto-generated method stub
BD bd = new BD(this);
return bd.buscar(rodada);
}
private ViewPager.SimpleOnPageChangeListener onPageChangeListener = new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
super.onPageSelected(position);
actionBar.setSelectedNavigationItem(position);
viewPager.setAdapter(new ViewPagerAdapter(getSupportFragmentManager(),preparaTab(position+1)));
}
};
private void addActionBarTabs() {
//actionBar = getSupportActionBar();
for (int i=0;i<rodadas;i++) {
ActionBar.Tab tab = actionBar.newTab().setText("Rodada "+(i+1))
.setTabListener(tabListener);
actionBar.addTab(tab);
}
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
}
private ActionBar.TabListener tabListener = new ActionBar.TabListener() {
@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
}
@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
}
};
}
ViewPagerAdapter.java
public class ViewPagerAdapter extends FragmentStatePagerAdapter {
private final int PAGES = 9;
private List<Jogo> jogoList;
public ViewPagerAdapter(FragmentManager fm, List<Jogo> jogoList) {
super(fm);
this.jogoList = jogoList;
}
@Override
public Fragment getItem(int position) {
return new TabFragment(jogoList);
}
@Override
public int getCount() {
return PAGES;
}
}
In the form of this code I ended up with the performance, because every time I go to the next TAB, the application hangs because a query is made to populate the listview of the next tab.