gpt4 book ai didi

android - Android View 寻呼机中的数据未更新

转载 作者:行者123 更新时间:2023-11-29 23:55:48 26 4
gpt4 key购买 nike

我有一个 fragment ,我在其中插入带有 View 寻呼机的选项卡布局。问题是当我在 onSuccess 方法中从服务器获取数据时,我的适配器没有更新数据。它在我的 onSuccess 方法被调用之前被初始化。我如何告诉我的适配器重新考虑我的数据?我试过 notifyDataSetChanged() 但它不起作用。

我的 fragment

public class CinemaFragmentLandingPage extends BaseHomeFragment implements View.OnClickListener, TabLayout.OnTabSelectedListener, CinemasContract.View {

private CinemasContract.Presenter mPresenterCinemas;

private ViewPager mViewPager;

private TabLayout mTabLayout;

private CinemaFragmentTabAdapter mCinemaTabAdapter;

private List<CinemasModel> mDataCinemas;

public static CinemaFragmentLandingPage newInstance() {
return new CinemaFragmentLandingPage();
}

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

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

mDataCinemas = new ArrayList<>();

mPresenterCinemas.getCinemas();

initViews(rootView);

return rootView;
}

private void initViews(View rootView) {
mTabLayout = rootView.findViewById(R.id.tab_layout);

mTabLayout.addTab(mTabLayout.newTab().setText(getResources().getString(R.string.cinemas)));

mTabLayout.addTab(mTabLayout.newTab().setText(getResources().getString(R.string.films)));

mViewPager = rootView.findViewById(R.id.pager);

mCinemaTabAdapter = new CinemaFragmentTabAdapter(this.getChildFragmentManager(), 2, mDataCinemas);

mViewPager.setAdapter(mCinemaTabAdapter);

mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(mTabLayout));

mTabLayout.setOnTabSelectedListener(this);

}

@Override
public void onClick(View view) {

}

@Override
public void onTabSelected(TabLayout.Tab tab) {
mViewPager.setCurrentItem(tab.getPosition());
}

@Override
public void onTabUnselected(TabLayout.Tab tab) {

}

@Override
public void onTabReselected(TabLayout.Tab tab) {

}

@Override
public void setLoading(boolean isActive) {
mHomeActivity.setLoadingDialog(isActive);
}

@Override
public void showGetCinemasSuccess(List<CinemasModel> cinemasModels) {
setLoading(false);
mDataCinemas = cinemasModels;
mViewPager.getAdapter().notifyDataSetChanged();
mViewPager.getAdapter().getItemPosition(0);
}

@Override
public void showGetCinemasFail(int errorCode) {
setLoading(false);
}

@Override
public void setPresenter(CinemasContract.Presenter presenter) {
mPresenterCinemas = Preconditions.checkNotNull(presenter);
}

我的适配器

public class CinemaFragmentTabAdapter extends FragmentStatePagerAdapter {

private int mNumOfTabs;

private ArrayList<CinemasModel> mCinemasData;

public CinemaFragmentTabAdapter(FragmentManager fm, int numOfTabs, List<CinemasModel> cinemasData) {
super(fm);
this.mNumOfTabs = numOfTabs;
this.mCinemasData = new ArrayList<CinemasModel>();
this.mCinemasData.addAll(cinemasData);
}

@Override
public Fragment getItem(int position) {
Fragment tab = null;
Bundle cinemasBundle = new Bundle();
cinemasBundle.putParcelableArrayList("cinemas", this.mCinemasData);

switch (position) {

case 0:
tab = new CinemaFragment();
tab.setArguments(cinemasBundle);
break;

case 1:
tab = new FilmsFragment();
tab.setArguments(cinemasBundle);
break;

default:
break;
}
return tab;
}

@Override
public int getCount() {
return mNumOfTabs;
}
}

我们将不胜感激。

最佳答案

首先在你的pojo类中定义构造函数,比如..

  public CinemasModel(String title, String description, String imageUrl, String author, String url) {

this.title = title;
this.description = description;
this.imageUrl = imageUrl;
this.author = author;
this.url = url;
}

然后更新这段代码。

 CinemasModel cinemas= new CinemasModel(title, description, url, imageUrl, author);

mDataCinemas.add(cinemas);
System.out.println("mDataCinemas size = " + mDataCinemas.size());

mCinemaTabAdapter = new inemaFragmentTabAdapter(getActivity(), mDataCinemas);
mViewPager.setAdapter(mCinemaTabAdapter );
mCinemaTabAdapter.notifyDataSetChanged();

注意:在 pojo 类中更新您的参数而不是 title、descriptio 等。我希望它对您有所帮助。

关于android - Android View 寻呼机中的数据未更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50383370/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com