How to update a Fragment that has a Gridview populated from Sqlite

前端 未结 3 635
不思量自难忘°
不思量自难忘° 2020-12-04 03:42

I have a ViewPager with two tabs which holds fragment. Inside the first fragment, I have a Gridview which is being populated with Sqlite Db.

I have an custom alertdi

3条回答
  •  误落风尘
    2020-12-04 04:23

    Here is a trick that i use to access Fragments inside a ViewPager in my custom viewPagerAdapter, i add two methods

    public class CustomViewPagerAdapter extends FragmentPagerAdapter {
        .....
        private String getFragmentTag(int viewId, int index) {
                return "android:switcher:" + viewId + ":" + index;
        }
    
        //mFragManager is a reference to FragmentManager
        public Fragment getFragmentByTag(int containerId, int position) {
            return mFragManager.findFragmentByTag(getFragmentTag(containerId, position));
        }
    }
    

    then in your activity or wherever you can access the customViewPager

    YourFragment frag = (YourFragment) customViewPager
                                .getFragmentByTag(YouViewPager.getId(), position);
    

    after that, add a method in YourFragment refreshData (if you like the name!) and in it refresh the grid Hope this help

提交回复
热议问题