我在MainActivity类中有一个SlideTabLayout。我在活动中执行异步网络操作,并将获取的结果传递给SlideTabLayout中的所需片段。但是适配器正在设置片段为空值,我的网络操作是因为我无法将值传递给片段,所以稍后获取数据。如果您可以看到下面的代码。在初始阶段,ArrayList值为0,并且Activity将空值传递给所有片段。然后在某个时间之后ArrayList值正在更新,不会传递给片段。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_meat);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
CatalogueService catalogueService = App42API.buildCatalogueService();
Intent categoryIntent = getIntent();
catalogueName = categoryIntent.getStringExtra("CATALOGUE");
viewPager = (ViewPager) findViewById(R.id.viewpager_meat);
tabLayout = (TabLayout) findViewById(R.id.tabs_meat);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MeatActivity.this,CartActivity.class);
startActivity(intent);
}
});
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
String chickenCategoryName = "Chicken";
catalogueService.getItemsByCategory(catalogueName, chickenCategoryName, new App42CallBack() {
@Override
public void onSuccess(Object o) {
Catalogue catalogue = (Catalogue) o;
for (int i = 0; i < catalogue.getCategoryList().size(); i++) {
String category = catalogue.getCategoryList().get(i).getName();
if (category.equals("Chicken")) {
chickenList = catalogue.getCategoryList().get(i).getItemList();
}
}
Log.e("Chicken Size 1",String.valueOf(chickenList.size()));
}
@Override
public void onException(Exception e) {
final String error = e.getMessage();
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MeatActivity.this,error,Toast.LENGTH_LONG).show();
}
});
}
});
String muttonCategoryName = "Mutton";
catalogueService.getItemsByCategory(catalogueName, muttonCategoryName, new App42CallBack() {
@Override
public void onSuccess(Object o) {
Catalogue catalogue = (Catalogue) o;
for (int i = 0; i < catalogue.getCategoryList().size(); i++) {
String category = catalogue.getCategoryList().get(i).getName();
if (category.equals("Mutton")) {
muttonList = catalogue.getCategoryList().get(i).getItemList();
break;
}
}
}
@Override
public void onException(Exception e) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MeatActivity.this,"Unable to fetch data",Toast.LENGTH_SHORT).show();
}
});
}
});
String seaFoodName = "Sea Foods";
catalogueService.getItemsByCategory(catalogueName, seaFoodName, new App42CallBack() {
@Override
public void onSuccess(Object o) {
Catalogue catalogue = (Catalogue) o;
for (int i = 0; i < catalogue.getCategoryList().size(); i++) {
String category = catalogue.getCategoryList().get(i).getName();
if (category.equals("Sea Foods")) {
seaFoodList = catalogue.getCategoryList().get(i).getItemList();
break;
}
}
}
@Override
public void onException(Exception e) {
}
});
setupViewPager(viewPager);
tabLayout.setupWithViewPager(viewPager);
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new OneFragment(activityId, chickenList), "CHICKEN");
Log.e("Chicken Size 2",String.valueOf(chickenList.size()));
adapter.addFragment(new TwoFragment(activityId,muttonList), "MUTTON");
adapter.addFragment(new ThreeFragment(activityId,seaFoodList), "SEAFOODS");
viewPager.setAdapter(adapter);
}