我正在使用ViewPager进行应用(它有3页),现在我正在尝试创建第一个页面来访问SQL数据库并在ListView中打印它(此功能仅适用于第一页,其他页面还有其他功能尚未实现),但现在我的问题是数据打印在错误的页面(第二页)。我正在使用getCurrentItem()
方法知道我在哪个页面。方法onCreateView
中的代码如下:
View rootView = inflater.inflate(R.layout.fragment_prioridad_alta, container, false);
List<String> array = new ArrayList<>();
ViewPager pg = (ViewPager) container;
int current = pg.getCurrentItem();
if (current == 0) {
// Database access, I'm using a Cursor for the DB and adding
// the data to an arrayList to print it using the ArrayAdapter,
// I don´t know if this is the most properly way of doing it
// but currently my main purpose is making this works
}
ArrayAdapter adapt = new ArrayAdapter(getActivity(), R.layout.fragment_prioridad_alta, R.id.textTarea, array);
ListView lv = (ListView) rootView.findViewById(R.id.listView);
lv.setAdapter(adapt);
return rootView;
为了清楚起见,我第一次运行应用程序时,数据打印在第一页(但只是第一次),之后当我更改页面时,数据仅打印在第二页,而且从不再次在第一次。
接下来,我为onCreate方法中实现的页面添加changeListener的代码:
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_prioridad_alta);
final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle("Prioridad Alta");
setSupportActionBar(toolbar);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener(){
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
switch(position){
// Camnbia la pagina de prioridad
case 0: toolbar.setTitle("Prioridad Alta"); break;
case 1: toolbar.setTitle("Prioridad Media"); break;
case 2: toolbar.setTitle("Prioridad Baja"); break;
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
这是PagerAdapter的代码:
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a PlaceholderFragment (defined as a static inner class below).
return PlaceholderFragment.newInstance(position + 1);
}
@Override
public int getCount() {
// Show 3 total pages.
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "Prioridad Alta";
case 1:
return "Prioridad Media";
case 2:
return "Prioridad Baja";
}
return null;
}
}
答案 0 :(得分:0)
使用
inspect
答案 1 :(得分:0)
因为您将位置传递给片段,所以检查您传递给片段而不是寻呼机选择索引的位置,因为片段在选择它们之前被初始化,而第0页第1页也被初始化。
所以而不是
int current =position -1;
使用您在newInstance(位置+1)
中创建Fragment时发送给片段的位置insert into app.coupon_customer(coup_id, cust_id)
values(coupon_id_value, IDENTITY_VAL_LOCAL())