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

Asked

Viewed 72 times

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, the menus are created normally, however, I also want to pass the id to 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 several ways and I couldn’t. Is it possible to do this in this code template? Thank you.

  • Where is the class code FragmentMain? It’s also strange the way you’re instilling it, it shouldn’t be fragment = FragmentMain().newInstance(...); without the new?

  • Add part of Fragmentmain to express myself better. Thank you.

  • Alter fragment = new FragmentMain().newInstance("1234", "Nome da Categoria"); for fragment = FragmentMain().newInstance("1234", "Nome da Categoria"); and see if it works.

  • Hello @ramaral, Fragmentmain loads normally, but what I want to do is the following: inside newInstance(...) I want you to load the ID and Category Name to then pass this data to Fragmentmain. This data: "1234", "Name of the Category" I can take normally for Fragmentemain, but the problem is to take this data coming from the TABS. Did you understand?

No answers

Browser other questions tagged

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