在我重新启动应用程序或切换到第三个TAB

时间:2016-05-10 09:33:34

标签: android android-tablayout

我的应用程序包含3个选项卡,从第一个选项卡我使用按钮转到新活动。在我的新活动中,我将数据保存到SQLite DATABASE。当我返回Tab时,它不会自动刷新。要在Tab上显示数据,我必须重新启动应用程序,或者当我移动到第3个选项卡时,1选项卡被销毁并在我返回第2个选项卡时重新创建。 那么如何刷新包含Tab的选项卡或Activity。 标签的代码是:

    public class MedListActivity extends AppCompatActivity {

    private Toolbar toolbar;
    private TabLayout tabLayout;
    private ViewPager viewPager;

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

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

        getSupportActionBar();

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

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

    private void setupViewPager(ViewPager viewPager) {
        ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
        adapter.addFragment(new HomeFragment(), "Home");
        adapter.addFragment(new PrescriptionFragment(), "Prescriptions");
        adapter.addFragment(new AccountSettingFragment(), "Accounts");
        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);
        }
    }
}

和我的Tab片段,我开始新的Activity。

    @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View view=inflater.inflate(R.layout.fragment_home, container, false);
    //       TextView timeText= (TextView)view.findViewById(R.id.textTime);
    //        String currentDateTimeString = DateFormat.getDateTimeInstance().format(new Date());
           // timeText.setText(currentDateTimeString);
            FloatingActionButton addBtn=(FloatingActionButton)view.findViewById(R.id.btnAdd);
            addBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent newActivity;
                    newActivity = new Intent(getActivity(), AddMedication.class);
                    getActivity().startActivity(newActivity);

                }
            });
recyclerViewPassed=(RecyclerView)view.findViewById(R.id.PastMedList);
        recyclerViewup=(RecyclerView)view.findViewById(R.id.UpMedList);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
        recyclerViewPassed.setLayoutManager(linearLayoutManager);
        LinearLayoutManager linearLayoutManager2 = new LinearLayoutManager(context);
        recyclerViewup.setLayoutManager(linearLayoutManager2);
        context=getActivity();
        databaseAdaptor=new DatabaseAdaptor(context);
        Calendar calendar = Calendar.getInstance();
        int day = calendar.get(Calendar.DAY_OF_WEEK);

        switch (day) {
            case Calendar.SUNDAY:
                daysName="Sun";
                break;

            case Calendar.MONDAY:
                daysName="Mon";
                break;

            case Calendar.TUESDAY:
                daysName="Tue";
                break;
            case Calendar.WEDNESDAY:
                daysName="Wed";
                break;

            case Calendar.THURSDAY:
                daysName="Thu";
                break;
            case Calendar.FRIDAY:
                daysName="Fri";
                break;
            case Calendar.SATURDAY:
                daysName="Sat";
                break;
        }
        Calendar cal= Calendar.getInstance();
        SimpleDateFormat simpleDateFormat= new SimpleDateFormat("hh:mm aa");
        initializeData();
        initializeAdapterpassed();
        initializeAdapterup();
        // Inflate the layout for this fragment
        return view;
           }



    private void initializeData() {
       databaseAdaptor.checkDaysName(daysName);
    }
    private void initializeAdapterpassed(){
        RVApassedAdapter adapter = new RVApassedAdapter(databaseAdaptor.passedLists);
       recyclerViewPassed.setAdapter(adapter);
    }
    private void initializeAdapterup(){
        RvUpAdapter adapter = new RvUpAdapter(databaseAdaptor.upList);
        recyclerViewup.setAdapter(adapter);
    }



}

如何在数据库中保存数据后自动刷新我的标签活动。

1 个答案:

答案 0 :(得分:0)

在MedListActivity中,添加以下代码:

@Override
public void onResume(){
    if(viewPager != null && viewPager.getAdapter() != null) {
        viewPager.getAdapter().notifyDataSetChanged();
    }
}

它应该刷新标签。

如果这不起作用,请执行以下操作:

  1. 将适配器创建机制(我假设这是您要显示的数据)移动到单独的方法,该方法将在OnCreate中调用。这不会改变目前的行为。
  2. 在片段中,覆盖onResume方法,并调用此新的适配器创建方法。
  3. 总结我们的评论讨论,最终的解决方案是在Fragment代码中添加以下方法:

    @Override 
    public void onResume() { 
        super.onResume(); 
        // Here we call the data setup methods again, to reflect 
        // the changes which took place then the Fragment was paused
        initializeData(); 
        initializeAdapterpassed(); 
        initializeAdapterup(); 
    }