As popular listiview of multiple Tabs with information coming from a BD Sqlite

Asked

Viewed 65 times

0

I am working on an app that presents all the games of the Brazilian championship.

I want each tab to show the games of a round, example:

tab1 (wheelset 1)

tab38 (wheel 38)

For this purpose I created tabs using Sherlock.

The games come from the Database I created in Sqlite.

My doubt is how to present the rounds on each tab?

I already have the class that looks for the comic dice and plays them on a list and then Adapter takes care of popular a Listview that is inside the tabs.

How to place a different round on each TAB?

Follow 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;
    }
}

The way this code ended the performance, because every time I slide to the next TAB the application hangs as it is made a query to popular the listview of the next tab.

  • You need to pass a list already with the games already selected for each TAB.

  • In theory I would return all the rounds at once and already mount the listviews and Fragments before the application finalizes the boot? Can you give me an example?

1 answer

0


In short I created an Asynctask that is called in onCreate, in it I import all the games. I created a class called round that gets a list of games. This way importing all the games at once, I won’t need to access the comic every time to create the list of the round solving the performance problem.

  • 2

    Was this the solution of the problem or a complement to the question? If it is the solution, could you detail more?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.