Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/217.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 视图寻呼机中的同一类/布局_Android_Android Viewpager_Fragment - Fatal编程技术网

Android 视图寻呼机中的同一类/布局

Android 视图寻呼机中的同一类/布局,android,android-viewpager,fragment,Android,Android Viewpager,Fragment,我试图多次将同一个类添加到viewpager,以显示从sqlite DB读取的不同信息。问题是在创建视图时,总是覆盖当前视图中的对象、文本视图等,而不是每个页面的片段中的对象、文本视图等 names = new ArrayList<String>(); for (int d : descritions) { Description temp = DBCompanies.getDescriptionById(db_path,

我试图多次将同一个类添加到viewpager,以显示从sqlite DB读取的不同信息。问题是在创建视图时,总是覆盖当前视图中的对象、文本视图等,而不是每个页面的片段中的对象、文本视图等

    names = new ArrayList<String>();
    for (int d : descritions) {
        Description temp = DBCompanies.getDescriptionById(db_path,
                GSSettings.DBCODE, d, Locale.getDefault().getLanguage(),
                GSSettings.DEFAULTLANGUAGE, false);
        names.add(temp.getTitle());

    }
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mSectionsPagerAdapter);
    // mViewPager.setCurrentItem(0);
    mViewPager.refreshDrawableState();
...
碎纸机

public class SectionsPagerAdapter extends FragmentPagerAdapter {
    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int i) {
        Description temp = DBCompanies.getDescriptionById(db_path,
                GSSettings.DBCODE, descritions[i], Locale.getDefault()
                        .getLanguage(), GSSettings.DEFAULTLANGUAGE, false);

        Fragment fragment = null;
        try {
            fragment = new CompanyLayout100();
            ((FragmentName) fragment).setName(temp.getTitle());
            Bundle bundle = new Bundle();
            bundle.putInt("descriptionId", temp.getId());
            fragment.setArguments(bundle);
        } catch (Exception e) {
        }

        return fragment;
    }

    @Override
    public int getCount() {
        return descritions.length;
        // return fragments.size();
    }

    @Override
    public CharSequence getPageTitle(int position) {
        String name = "";
        try {
            name = names.get(position);
        } catch (Exception e) {
        }
        return name;
    }
}

现在的情况是,viewpager为这些帧创建视图,但总是在可见帧中输入数据。我认为是因为layout.xml是相同的,所以只有一个id,所有对象都指向可见的布局。

您需要一个FragmentPagerAdapter来创建实例

public class PagerAdapter extends FragmentPagerAdapter {

    int pages;

    public PagerAdapter(FragmentManager manager) {
        super(manager);
    }

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

    @Override
    public Fragment getItem(int position) {
        return MyFragment.newInstance(position);
    }

    public void setPages(int no) {
        pages = no;
    }
}
因此,在主要活动中,请附加PagerAdapter

   adapter = new PagerAdapter(getSupportFragmentManager());
   adapter.setPages(noPages);
   pager.setAdapter(adapter);
最后,您需要包含newInstance方法的片段。查看此以了解更多信息

干杯

编辑#1:MyFragment注意:必须将位置作为参数传递

public class MyFragment extends SherlockFragment {
  private static final String KEY_POSITION="position";

  static MyFragment newInstance(int position) {
    MyFragment frag=new MyFragment();
    Bundle args=new Bundle();

    args.putInt(KEY_POSITION, position);
    frag.setArguments(args);

    return(frag);
  }

  @Override
  public View onCreateView(LayoutInflater inflater,
                           ViewGroup container,
                           Bundle savedInstanceState) {
    View result=inflater.inflate(R.layout.editor, container, false);
    EditText editor=(EditText)result.findViewById(R.id.editor);
    int position=getArguments().getInt(KEY_POSITION, -1);

    editor.setHint(String.format(getString(R.string.hint), position + 1));

    return(result);
  }
}

我有一个fragmentpageadapter,与您的建议不同的是,我创建了片段列表,并在创建适配器时传递它,mSectionsPagerAdapter=new SectionsPagerAdapter(getSupportFragmentManager(),fragments);然后我返回片段,public fragment getItem(int position){return fragments.get(I);}您的意思是应该在FragmentPagerAdapter中创建片段吗?这是我在许多示例中看到的方式,也是我实现它的方式。很好用。这个示例看起来更简单:谢谢,我将尝试更改创建对象/帧的位置。就我的理解,为什么会这样?如果我将带有帧的数组传递给FragmentPagerAdapter,那么如果FragmentPagerAdapter在Fragment getItem(int position)方法中创建帧会有什么不同?您好,我必须进行检查,但我认为我的错误是使用init()中的getActivity().findByIDd(…)。所以我在活动视图中输入数据,这是可见的片段。。。。我想我必须使用getview().findById(..)我稍后会检查
   adapter = new PagerAdapter(getSupportFragmentManager());
   adapter.setPages(noPages);
   pager.setAdapter(adapter);
public class MyFragment extends SherlockFragment {
  private static final String KEY_POSITION="position";

  static MyFragment newInstance(int position) {
    MyFragment frag=new MyFragment();
    Bundle args=new Bundle();

    args.putInt(KEY_POSITION, position);
    frag.setArguments(args);

    return(frag);
  }

  @Override
  public View onCreateView(LayoutInflater inflater,
                           ViewGroup container,
                           Bundle savedInstanceState) {
    View result=inflater.inflate(R.layout.editor, container, false);
    EditText editor=(EditText)result.findViewById(R.id.editor);
    int position=getArguments().getInt(KEY_POSITION, -1);

    editor.setHint(String.format(getString(R.string.hint), position + 1));

    return(result);
  }
}