Dynamic fragment

Asked

Viewed 41 times

1

I have the following problem when the screen is changed to 3 tabs and then back to 2 tabs these two tabs are blank without content anyone could help me? The code is not showing any error, when I return to the main Activity the logcat history does not report any error!

public class Cid10 extends AppCompatActivity {

    public SectionsPagerAdapter mSectionsPagerAdapter;


    public ViewPager mViewPager;


    public Toolbar toolbar;
    TabLayout tabLayout;
    int j = 0;

    // Favorites favorites = new Favorites();
    Buscar buscar = new Buscar();
    //Chapters cid = new Chapters();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        SubCategory subCategory = new SubCategory();

        subCategory.listobjectSubAndCategory.clear();

        subCategory.Consult("SELECT id, code, description FROM subcategories where favorite = 1");


        if (subCategory.listobjectSubAndCategory.size() > 0) {

            j = 1;

            setContentView(R.layout.activity_menu);

            toolbar = findViewById(R.id.toolbar);
            mViewPager = findViewById(R.id.container);

            tabLayout = findViewById(R.id.tabs);

        } else {

            j = 2;

            setContentView(R.layout.activity_menu2);
            toolbar = findViewById(R.id.toolbar2);
            mViewPager = findViewById(R.id.container2);

            tabLayout = findViewById(R.id.tabs2);

        }


        setSupportActionBar(toolbar);
        // Create the adapter that will return a fragment for each of the three
        // primary sections of the activity.
        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());


        // Set up the ViewPager with the sections adapter.
        mViewPager.setAdapter(mSectionsPagerAdapter);

        mSectionsPagerAdapter.notifyDataSetChanged();
        mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
        tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager));

        BancoOpenHelper bancoOpenHelper = new BancoOpenHelper(getApplication());

        try {
            bancoOpenHelper.createDataBase();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.

        getMenuInflater().inflate(R.menu.trash, menu);

        if (buscar.BeforeOption() == 2) {

            menu.findItem(R.id.action_trash).setVisible(true);

        } else {

            menu.findItem(R.id.action_trash).setVisible(false);


        }

        buscar.mMenu = menu;
        return true;

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        switch (item.getItemId()) {
            case R.id.action_trash:


                new AlertDialog.Builder(this)

                        .setMessage("Deseja apagar o histórico de CID pesquisados recentemente?")
                        .setPositiveButton("Sim",
                                new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialogInterface, int i) {
                                        buscar.DeleteList();
                                    }

                                })
                        .setNegativeButton("não", null)
                        .show();
        }


        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {
        /**
         * The fragment argument representing the section number for this
         * fragment.
         */
        private static final String ARG_SECTION_NUMBER = "section_number";

        public PlaceholderFragment() {
        }

        /**
         * Returns a new instance of this fragment for the given section
         * number.
         */
        public static PlaceholderFragment newInstance(int sectionNumber) {
            PlaceholderFragment fragment = new PlaceholderFragment();
            Bundle args = new Bundle();
            args.putInt(ARG_SECTION_NUMBER, sectionNumber);
            fragment.setArguments(args);
            return fragment;
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {


            View rootView = inflater.inflate(R.layout.fragment_menu, container, false);

            return rootView;
        }


    }


    /**
     * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
     * one of the sections/tabs/pages.
     */
    public class SectionsPagerAdapter extends FragmentPagerAdapter {

        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            // getItem is called to instantiate the fragment for the given page.
            // Return a PlaceholderFragment (defined as a static inner class below).


            if (j == 1) {

                switch (position) {

                    case 0:

                        return new Favorites();


                    case 1:

                        buscar = new Buscar();
                        return buscar;


                    case 2:

                        return new Chapters();


                    default:
                        return buscar;
                }
            } else {

                switch (position) {

                    case 0:

                        //System.out.println(buscar);

                        buscar = new Buscar();
                        return buscar;


                    case 1:

                        return new Chapters();


                    default:
                        return buscar;
                }

            }
        }


        @Override
        public int getCount() {
            // Show 3 total pages.
            if (j == 1) {
                return 3;
            } else {

                return 2;

            }

        }

    }


    @SuppressLint("WrongConstant")
    public void ExecSql(String sql) {
        SQLiteDatabase conection;

        String path = "/data/data/cid10pro.medphone.br.com.medphone/database/cid10banco";
        conection = SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.OPEN_READWRITE);
        conection.execSQL(sql);

    }


    @Override
    protected void onResume() {
        super.onResume();

        //  if (j == 0) {
        System.out.println("????????????????????/////////  onRESUME  ////////////??????????????????????");


        SubCategory subCategory = new SubCategory();

        subCategory.listobjectSubAndCategory.clear();

        subCategory.Consult("SELECT id, code, description FROM subcategories where favorite = 1");

        if (subCategory.listobjectSubAndCategory.size() < 1) {

            if (j == 2) {

            } else {

                j = 2;

                setContentView(R.layout.activity_menu2);
                    toolbar = null;
                    toolbar = findViewById(R.id.toolbar2);
                    mViewPager = null;
                    mViewPager = findViewById(R.id.container2);
                    tabLayout = null;

                    tabLayout = findViewById(R.id.tabs2);

                    setSupportActionBar(toolbar);
                    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());


                //mViewPager.clearOnPageChangeListeners();

                 mViewPager.setAdapter(mSectionsPagerAdapter);
                 mViewPager.setOffscreenPageLimit(2);

                mSectionsPagerAdapter.notifyDataSetChanged();
                mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
                mSectionsPagerAdapter.notifyDataSetChanged();
                tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager));
                mSectionsPagerAdapter.notifyDataSetChanged();



            }

        } else {

            if (j == 1) {

            } else {

                j = 1;
                setContentView(R.layout.activity_menu);
                toolbar = null;
                toolbar = findViewById(R.id.toolbar);
                mViewPager = null;
                mViewPager = findViewById(R.id.container);

                tabLayout = null;
                tabLayout = findViewById(R.id.tabs);

                setSupportActionBar(toolbar);
                mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

                mViewPager.clearOnPageChangeListeners();

                mViewPager.setAdapter(mSectionsPagerAdapter);
                mSectionsPagerAdapter.notifyDataSetChanged();

                mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
                mSectionsPagerAdapter.notifyDataSetChanged();
                tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager));
                mSectionsPagerAdapter.notifyDataSetChanged();
            }
            //  }

        }
    }
}
No answers

Browser other questions tagged

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