0

Here is a tab for 2 fragment, at the 1st fragment click button_save it will perform save data and stay at current page, but when I tab to 2nd page which is 2nd fragment, the data wont update then i need to close my application and open back to the 2nd page fragment then only the data being update, how can i solve this problem that i no need close my application when tab to 2nd page and getting auto update the data in 2nd page ? use Intent ?

the code below is from 1st fragment

 public class KeyInWeightF extends Fragment implements View.OnClickListener {


        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            contentView=inflater.inflate(R.layout.daily_weight_fragement, container, false);


            btnSave = (Button) contentView.findViewById(R.id.button_save);

            dbconnection = new SQLControlerWeight(getActivity());
            dbconnection.openDatabase();
            btnSave.setOnClickListener(this);

            return contentView;
        }

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

        }

        @Override
        public void onClick(View v){

            switch (v.getId()){


                case R.id.button_save:

                        if (btnTime.getText().toString().equals("")||btnDate.getText().toString().equals("")
                    ||kgnum.getText().toString().equals("")||bf.getText().toString().equals("")){
                            Toast.makeText(getActivity(), "Please insert all the detail!", Toast.LENGTH_LONG).show();
                        }
                        else{
                            String kgn = kgnum.getText().toString()+" kg";
                            String bodyfat = bf.getText().toString()+" %";
                            String date = btnDate.getText().toString();
                            String time = btnTime.getText().toString();
                            String comment = comm.getText().toString();
                            dbconnection.insertNote(kgn, bodyfat, date, time, comment);
                            Toast.makeText(getActivity(), "Suggest", Toast.LENGTH_LONG).show();

                          // Did i need add some code "Intent "here ??
                        }


                    break;
        }

    }
     }

This is the 2nd fragment

   public class HistoryF  extends Fragment implements YourFragmentInterface{

    View contentView;
    ListView list;
    SQLControlerWeight dbconnection;
    TextView weight_num, date_num, time_num, bf_num, comment,weight_ID;
    private SimpleCursorAdapter adapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        contentView = inflater.inflate(R.layout.history_fragment, container, false);

        dbconnection = new SQLControlerWeight(getActivity());
        dbconnection.openDatabase();
        list = (ListView) contentView.findViewById(R.id.listViewWeight);


        list.setAdapter(adapter);


        list.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView parent, View view, int position, long id) {
                weight_ID = (TextView) view.findViewById(R.id.weight_id);
                weight_num = (TextView) view.findViewById(R.id.weight_num);
                bf_num = (TextView) view.findViewById(R.id.bf_num);
                date_num = (TextView) view.findViewById(R.id.date_num);
                time_num = (TextView) view.findViewById(R.id.time_num);
                comment = (TextView) view.findViewById(R.id.comment_text);

                String weightId = weight_ID.getText().toString();
                String wn = weight_num.getText().toString();
                String bfn = bf_num.getText().toString();
                String dn = date_num.getText().toString();
                String tn = time_num.getText().toString();
                String cm = comment.getText().toString();

                Intent modify_intent = new Intent(getActivity(), Update_detail_info.class);

                modify_intent.putExtra("weightId", weightId);
                modify_intent.putExtra("dateNum", dn);
                modify_intent.putExtra("timeNum", tn);
                modify_intent.putExtra("weightNum", wn);
                modify_intent.putExtra("bodyFatNum", bfn);
                modify_intent.putExtra("comment", cm);

                startActivity(modify_intent);
            }
        });

    return contentView;
    }
    @Override
    public void fragmentBecameVisible() {

        Cursor cursor = dbconnection.readNote();
        String[] from = new String[]{
                DBHelperNote.WEIGHT_ID,
                DBHelperNote.WEIGHT_NUM,
                DBHelperNote.BODY_FAT,
                DBHelperNote.WEIGHT_DATE,
                DBHelperNote.WEIGHT_TIME,
                DBHelperNote.WEIGHT_COMMENTS

        };
        int[] to = new int[]{
                R.id.weight_id,
                R.id.weight_num,
                R.id.bf_num,
                R.id.date_num,
                R.id.time_num,
                R.id.comment_text
        };

        adapter = new SimpleCursorAdapter(
                contentView.getContext(), R.layout.history, cursor, from, to,0);

        adapter.notifyDataSetChanged();
    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }


}

this is my main activity :

public class MainActivity extends AppCompatActivity {

    Toolbar toolbar;
    TabLayout tabLayout;
    ViewPager viewPager;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        viewPager = (ViewPager) findViewById(R.id.viewpager);
        setupViewPager(viewPager);

        tabLayout = (TabLayout) findViewById(R.id.tablayout);
        tabLayout.setupWithViewPager(viewPager);


    }
    private void setupViewPager(ViewPager viewPager) {
        ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
        adapter.addFragment(new KeyInWeightF(), "TRACK");
        adapter.addFragment(new HistoryF(), "HISTORY");
        adapter.addFragment(new AnalysisF(), "GRAPH");
        viewPager.setAdapter(adapter);
    }

    class ViewPagerAdapter extends FragmentPagerAdapter {
        private final List<Fragment> mFragmentList = new ArrayList<>();
        private final List<String> mFragmentTitleList = new ArrayList<>();

        public ViewPagerAdapter(FragmentManager manager) {
            super(manager);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragmentList.get(position);
        }

        @Override
        public int getCount() {
            return mFragmentList.size();
        }

        public void addFragment(Fragment fragment, String title) {
            mFragmentList.add(fragment);
            mFragmentTitleList.add(title);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mFragmentTitleList.get(position);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

Interface:

public interface YourFragmentInterface {
    void fragmentBecameVisible();
}
Wg Sam
  • 61
  • 1
  • 2
  • 11

1 Answers1

0

Sorry onResume() may not work correctly for tabs.In think you can implement the answer explained in this question.

create an interface as shown below:

public interface YourFragmentInterface {
    void fragmentBecameVisible();
}

Implement above interface in your 2nd fragmnet as shown below:

public class HistoryF  extends Fragment implements YourFragmentInterface { 

    View contentView;
    ListView list;
    SQLControlerWeight dbconnection;
    TextView weight_num, date_num, time_num, bf_num, comment,weight_ID;
    private SimpleCursorAdapter adapter; 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        contentView = inflater.inflate(R.layout.history_fragment, container, false);

        dbconnection = new SQLControlerWeight(getActivity());
        dbconnection.openDatabase();
        list = (ListView) contentView.findViewById(R.id.listViewWeight);


        list.setAdapter(adapter);


        list.setOnItemClickListener(new OnItemClickListener() {
            @Override 
            public void onItemClick(AdapterView parent, View view, int position, long id) {
                weight_ID = (TextView) view.findViewById(R.id.weight_id);
                weight_num = (TextView) view.findViewById(R.id.weight_num);
                bf_num = (TextView) view.findViewById(R.id.bf_num);
                date_num = (TextView) view.findViewById(R.id.date_num);
                time_num = (TextView) view.findViewById(R.id.time_num);
                comment = (TextView) view.findViewById(R.id.comment_text);

                String weightId = weight_ID.getText().toString();
                String wn = weight_num.getText().toString();
                String bfn = bf_num.getText().toString();
                String dn = date_num.getText().toString();
                String tn = time_num.getText().toString();
                String cm = comment.getText().toString();

                Intent modify_intent = new Intent(getActivity(), Update_detail_info.class);

                modify_intent.putExtra("weightId", weightId);
                modify_intent.putExtra("dateNum", dn);
                modify_intent.putExtra("timeNum", tn);
                modify_intent.putExtra("weightNum", wn);
                modify_intent.putExtra("bodyFatNum", bfn);
                modify_intent.putExtra("comment", cm);

                startActivity(modify_intent);
            } 
        }); 

    return contentView;
    } 

    @Override 
    public void fragmentBecameVisible() { 

        Cursor cursor = dbconnection.readNote();
        String[] from = new String[]{
                DBHelperNote.WEIGHT_ID, 
                DBHelperNote.WEIGHT_NUM, 
                DBHelperNote.BODY_FAT, 
                DBHelperNote.WEIGHT_DATE, 
                DBHelperNote.WEIGHT_TIME, 
                DBHelperNote.WEIGHT_COMMENTS 

        }; 
        int[] to = new int[]{
                R.id.weight_id,
                R.id.weight_num,
                R.id.bf_num,
                R.id.date_num,
                R.id.time_num,
                R.id.comment_text
        }; 

        adapter = new SimpleCursorAdapter( 
                contentView.getContext(), R.layout.history, cursor, from, to,0);

        adapter.notifyDataSetChanged(); 
    } 
    @Override 
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    } 

and in your MainActivity in onCreate() do the following:

@Override 
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true); 

        viewPager = (ViewPager) findViewById(R.id.viewpager);
        setupViewPager(viewPager);

        tabLayout = (TabLayout) findViewById(R.id.tablayout);
        tabLayout.setupWithViewPager(viewPager);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override 
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            } 

            @Override 
            public void onPageSelected(int position) {
                 YourFragmentInterface fragment = (YourFragmentInterface)              mPagerAdapter.instantiateItem(mViewPager, position);
                if (fragment != null) {
                    fragment.fragmentBecameVisible();
                } 
            } 

            @Override 
            public void onPageScrollStateChanged(int state) {

            } 
        }); 

    } 

On implementing the interface you will have the function fragmentBecameVisible().

so whenever your fragment is active it will call the function fragmentBecameVisible(). which will update your tab. I think this solves your problem

Community
  • 1
  • 1
Nabeel K
  • 5,938
  • 11
  • 38
  • 68
  • I still cant get your meaning ,is that i need to move the code above and put as another function ? but where the fragmentBecameVisible() function i need to put in my code ? – Wg Sam Dec 12 '15 at 14:44
  • create an interface as mentioned in the answer I have given. and on implementing you will have the above function. – Nabeel K Dec 12 '15 at 14:46
  • can u check for me, i aldy upload the post – Wg Sam Dec 12 '15 at 15:09
  • i aldy update my post and i aldy follow your guild, but the last viewpager code where should i add ? in my main activity ? but my main activity viewpager code is diffrent from that , and sorry for forget to post my main activity code, please hlp – Wg Sam Dec 13 '15 at 06:00
  • have you got the answer ? – Nabeel K Dec 14 '15 at 05:28
  • i just update my mainactivity post, you guild me that to put the code into onCreateView() at my MainActivity, but my mainactivity for ViewPagerAdapter is different from you gv me the code, so i have no idea to put the code or modify it and put inside my mainActivity ? can you take a look at my mainActivity and show me the code again? i belive we almost there ha – Wg Sam Dec 14 '15 at 06:56
  • It was a mistake. its just the onCreate() method of your MainActivity not onCreatView() – Nabeel K Dec 14 '15 at 07:16
  • so you mean that the code for my onCreateView still remain the same, the only way i need to add for your code is inside the onCreate() method ? – Wg Sam Dec 14 '15 at 08:16
  • get error for this mPagerAdapter.instantiateItem(mViewPager, position); – Wg Sam Dec 14 '15 at 08:26