How to pass variables from JSON to Fragment in android application using TABS?

0

I have the following code:

public class ActivityCategorias  extends AppCompatActivity implements MaterialTabListener{


    private List<Category> categoriesList;

    MaterialTabHost tabHost;
    ViewPager pager;
    ViewPagerAdapter adapter;
    private Toolbar toolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_text);

        //Lollipop Style
        Utils.setStatusBarcolor(getWindow(), getResources().getColor(R.color.primary_dark));
        if (Utils.isLollipop())
            findViewById(R.id.toolbar).setVisibility(View.GONE);

        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setHomeButtonEnabled(true);
        }

        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

        //Get and Set the Post Title
        String itemTitle = "Categorias";
        setTitle(Html.fromHtml(itemTitle));


        tabHost = (MaterialTabHost) this.findViewById(R.id.tabHost);
        pager = (ViewPager) this.findViewById(R.id.pager);

        // init view pager
        adapter = new ViewPagerAdapter(getSupportFragmentManager());
        pager.setAdapter(adapter);
        pager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {

                Toast.makeText(ActivityCategorias.this,
                        "Selected page position: " + position, Toast.LENGTH_SHORT).show();
                tabHost.setSelectedNavigationItem(position);

            }

        });

        categoriesList = AppController.getInstance().getPrefManger().getCategories();

        for (Category a : categoriesList) {


            //retorna a.getId() e a.getTitle()

            tabHost.addTab(
                    tabHost.newTab()
                            .setText(a.getTitle())
                            .setTabListener(this)
            );


        }
    }

    @Override
    public void onTabSelected(MaterialTab tab) {
        pager.setCurrentItem(tab.getPosition());


    }

    @Override
    public void onTabReselected(MaterialTab tab) {

    }

    @Override
    public void onTabUnselected(MaterialTab tab) {

    }

    private class ViewPagerAdapter extends FragmentStatePagerAdapter {

        public ViewPagerAdapter(FragmentManager fm) {
            super(fm);

        }

        public Fragment getItem(int position) {


            Fragment fragment = null;


            fragment = new FragmentMain().newInstance("1234", "Teste");


            return fragment;

        }

        @Override
        public int getCount() {
            return 5;
        }


        @Override
        public CharSequence getPageTitle(int position) {
            return "Tab " + position;
        }


    }


}

This part of the code returns an array of categories and creates the menus and returns the ID and Category Name:

categoriesList = AppController.getInstance().getPrefManger().getCategories();

        for (Category a : categoriesList) {
            //retorna a.getId() e a.getTitle()
            tabHost.addTab(
                    tabHost.newTab()
                            .setText(a.getTitle())
                            .setTabListener(this)
            );
        }
    }

As we can see, menus are created normally, however, I also want to pass the id to the Fragment, ex in this line:

fragment = new FragmentMain().newInstance("1234", "Nome da Categoria");

Part of the FragmentMain class:

public class FragmentMain extends Fragment {

    private static final String TAG = FragmentMain.class.getSimpleName();

    public static final String bundleCategoryId = "categoryId";
    public static final String bundleCategoryName = "categoryName";

    public FragmentMain() {

    }

    public static FragmentMain newInstance(String categoryId, String categoryName) {
        FragmentMain f = new FragmentMain();
        Bundle args = new Bundle();
        args.putString(bundleCategoryId, categoryId);
        args.putString(bundleCategoryName, categoryName);
        f.setArguments(args);
        return f;
    }

I tried it in many ways and I could not. Is it possible to do this in this code template? Thank you.

    
asked by anonymous 07.05.2015 / 20:10

0 answers