我在第二个标签上的tabclick监听器上更新我的viewpager时出现问题" CART",我想更新名为" CART"的第二个片段中的列表视图通过从数据库表中获取记录......
这是我在MainActivity中的代码:
tabLayout.setOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(viewPager){
@Override
public void onTabSelected(TabLayout.Tab tab) {
super.onTabSelected(tab);
int tab_index = tab.getPosition();
if (tab_index == 1){
adapter.updateData();
// adapter.notifyDataSetChanged();
}
}
}`private void setupViewPager(ViewPager viewPager) {
adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new OneFragment(), "HOME");
adapter.addFragment(new TwoFragment(), "CART");
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentStatePagerAdapter {
public final List<Fragment> mFragmentList = new ArrayList<>();
public final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
public void updateData(){
notifyDataSetChanged();
}
@Override
// Return the Fragment associated with a specified position..... Android calls getItem(...) only if it wants to create new Fragments.
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
//Return the number of views available.
public int getCount() {
return mFragmentList.size();
}
@Override
// Called when the host view is attempting to determine if an item's position has changed.
// Returns POSITION_UNCHANGED if the position of the given item has not changed or POSITION_NONE if the item is no longer present in the adapter.
public int getItemPosition(Object object) {
return POSITION_NONE;
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}`
片段2中的代码,即CART:
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
DM = DB_Object.getInstance(getActivity());
View view = inflater.inflate(R.layout.fragment_two,container,false);
listview_product = (ListView) view.findViewById(R.id.product_list);
BindListView(DM);
return view;
}
public void set_order_set_id(boolean order,String id){ is_order = order; unique_id = id; }
public void BindListView(DataManipulator DM_Obj)
{
BindProducts = new ArrayList<ProductList>();
ProductDetails = new ArrayList<String[]>();
ProductDetails = DM_Obj.GetFromInventory();
product_barcode = new String[ProductDetails.size()];
product_price = new String[ProductDetails.size()];
int i = 0;
for(String[] product : ProductDetails )
{
product_barcode[i] = product[0];
product_price[i] = product[1];
BindProducts.add(new ProductList(product_barcode[i],product_price[i]));
i++;
}
if (getActivity() != null) {
list_adapter = new ListAdapter(getActivity(), BindProducts);
// listview_product = (ListView) getView().findViewById(R.id.product_list);
listview_product.setAdapter(list_adapter);
// listview_product.invalidate();
}
}