viewpageradapter和livedata

2j4z5cfb  于 2021-06-29  发布在  Java
关注(0)|答案(0)|浏览(275)

我有一个viewpageradapter,每天一页,每页一个listview。我正试着把livedata加入其中。
现在,它正在这样做:
打开应用程序
i/oncreateview:startdate=1608958800 items.size()=0
i/oncreateview:startdate=1608786000项。size()=0
i/onchanged:startdate=1608786000 items.size()=2
i/onchanged:startdate=1608786000 items.size()=2
我看到今天的名单,应该有两个项目,它是准确的。
向右滑动
i/oncreateview:startdate=1607922000项。size()=2
i/onchanged:startdate=1607922000 items.size()=2
我看到前一天的清单,应该有4个项目,它有今天的清单。
再次向右滑动
i/oncreateview:startdate=1607835600 items.size()=0
i/onchanged:startdate=1607835600 items.size()=0
我前天看到的,应该有4个项目,而且是空的。
向左滑动
i/oncreateview:startdate=1608958800 items.size()=0
i/onchanged:startdate=1608958800 items.size()=0
昨天的名单上还有今天的数据。
再次向左滑动
没有日志,今天的名单也被销毁了。
下面是我片段中的一些代码:

if (bundle != null) {
            sTitle = bundle.getString("title");
            setTitle(sTitle);
            year = 1900 + bundle.getInt("year");
            month = bundle.getInt("month");
            day = bundle.getInt("day");
            Calendar calendar = Calendar.getInstance();
            calendar.set(year, month, day, 0, 0, 0);
            startDate = (int) (calendar.getTimeInMillis() / 1000L);
        }

        dataViewModel = new ViewModelProvider(this, new dataViewModelFactory(getActivity().getApplication(), startDate)).get(dataViewModel.class);

        List<listItems> items = dataViewModel.getDataByDate().getValue();
        if (items == null)
            items = new ArrayList<>();
        Log.i("OnCreateView", "startDate = " + startDate + " items.size() = " + items.size());
        MyArrayAdapter adapter = new MyArrayAdapter(this.getActivity(), items);

        theListView.setAdapter(adapter);

        dataViewModel.getDataByDate().observe(getViewLifecycleOwner(), new Observer<List<data>>() {
            @Override
            public void onChanged(List<listItems> data) {
                List<listItems> items = data;
                if (items == null)
                    items = new ArrayList<>();
                Log.i("OnChanged", "startDate = " + startDate + " items.size() = " + items.size());
                adapter.setExercises(items);
            }
        });

从我的myarrayadapter:

public void setExercises(List<listItems> mListItems) {
    this.clear();
    this.addAll(mListItems);
    notifyDataSetChanged();
}

以及我的viewmodel:

public class DataViewModel extends AndroidViewModel {
    private Repository repository;
    Integer startDate;
    public static LiveData<List<listItems>> mItems = null;

    public DataViewModel(Application application, Integer startDate) {
        super(application);
        this.startDate = startDate;
        repository = new Repository(application);
    }

    public LiveData<List<listItems>> getDataByDate() {
        if (mItems == null) {
            mItems = repository.getDataByDate(startDate);
        }
        return mItems;
    }

}

以及我的viewmodelfactory:

public class DataViewModelFactory implements ViewModelProvider.Factory {
    private Application mApplication;
    private Integer mParam;

    public DataViewModelFactory(Application application, Integer param) {
        mApplication = application;
        mParam = param;
    }

    @Override
    public <T extends ViewModel> T create(Class<T> modelClass) {
        return (T) new DataViewModel(mApplication, mParam);
    }
}

从我的基本活动来看:

public void ShowCards(Map<Integer, Long> map, Class<?> fragment) {

    Log.i("BaseActivity", "ShowCards" );

    Integer started;
    Long id;

    for (Map.Entry<Integer, Long> entry : map.entrySet()) {

        started = entry.getKey();
        id = entry.getValue();

        Log.i("BaseActivity: ShowCards", "started(" + started + ") id (" + id + ") [3]");

        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(started * 1000L);

        addPage(id, calendar.get(Calendar.YEAR) - 1900, calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), fragment);

    }

}

public void addPage(Long id, int year, int month, int day, Class<?> fragment) {
    Log.i("BaseActivity", "addPage: fragment: " + fragment.getName());
    Log.i("BaseActivity", "addPage: id: " + id);
    Log.i("BaseActivity", "addPage: year: " + year);
    Log.i("BaseActivity", "addPage: month: " + month);
    Log.i("BaseActivity", "addPage: day: " + day);
    Bundle bundle = new Bundle();
    SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);    //TODO: Put dateFormat in the string table.
    Date date = new Date(year, month, day);
    String pagename = sdf.format(date);
    bundle.putString("title", pagename);
    bundle.putInt("year", year);
    bundle.putInt("month", month);
    bundle.putInt("day", day);
    bundle.putLong("id", id);
    BaseFragment myFragment = null;
    try {
        myFragment = (BaseFragment) fragment.newInstance();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (InstantiationException e) {
        e.printStackTrace();
    }
    Log.i("BaseActivity", "addPage: myFragment: " + myFragment.toString() + " bundle = " + bundle.toString() + "[4]");
    myFragment.setArguments(bundle);
    viewPagerAdapter.addFrag(myFragment, pagename, id);
    viewPagerAdapter.notifyDataSetChanged();
    Log.i("BaseActivity: addPage", "viewPageAdapter.getCount() = " + viewPagerAdapter.getCount());

    viewPager.setCurrentItem(viewPagerAdapter.getCount() - 1);

}

我的viewpageradapter:

public class ViewPagerAdapter extends FragmentStatePagerAdapter {
    private final ArrayList<Fragment> mFragmentList = new ArrayList<>();
    private final ArrayList<Long> mFragmentIdList = new ArrayList<>();
    private final ArrayList<String> mFragmentTitleList = new ArrayList<>();
    Context context;
    ViewPager viewPager;

    public ViewPagerAdapter(FragmentManager manager, Context context, ViewPager viewPager) {
        super(manager);
        this.context = context;
        this.viewPager = viewPager;
    }

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

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

    public void addFrag(Fragment fragment, String title, Long Id) {
        mFragmentList.add(fragment);
        mFragmentTitleList.add(title);
        mFragmentIdList.add(Id);
        notifyDataSetChanged();
    }

谢谢你的帮助。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题