Android ArrayList显示它';s的大小为0,即使在向其添加元素后也是如此

Android ArrayList显示它';s的大小为0,即使在向其添加元素后也是如此,android,arraylist,Android,Arraylist,我有一个ArrayList来存储片段的实例。此名为“listofFragInstances”的ArrayList是全局的。我已在setUpViewPager()方法中将值添加到此ArrayList。 但是当在setUpViewPager()方法之外的另一个方法中使用它时,它的大小在使用调试器时显示为零,而在setUpViewPager()方法中它的大小与预期的一样。有什么问题吗 @Override protected void onCreate(Bundle savedInstanceS

我有一个ArrayList来存储片段的实例。此名为“listofFragInstances”的ArrayList是全局的。我已在
setUpViewPager()
方法中将值添加到此ArrayList。 但是当在
setUpViewPager()
方法之外的另一个方法中使用它时,它的大小在使用调试器时显示为零,而在
setUpViewPager()
方法中它的大小与预期的一样。有什么问题吗

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.audit_form_activity);   

        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);


        tabLayout = (TabLayout) findViewById(R.id.tabLayout);

        button = (Button) findViewById(R.id.button);

        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        viewPager = (ViewPager) findViewById(R.id.viewpager);

        setupViewPager(viewPager);


        viewPager.setOffscreenPageLimit(no_of_categories);


        tabLayout = (TabLayout) findViewById(R.id.tabLayout);
        tabLayout.setupWithViewPager(viewPager);


    }


    private void setupViewPager(ViewPager viewPager) {

        //       Outside Loop
        ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());

//        Get the JSON Data from DataSource


        try {
            JSONObject pageObj = new JSONObject(page);
            JSONArray auditQuestionsArray = pageObj.getJSONArray("AuditQuestions");
            int auditQuestionsArrayLength = auditQuestionsArray.length();
            no_of_categories = auditQuestionsArrayLength;


//        Outer-Loop Through the CategoryNames to Create Fragments for each CategoryName.

            for (int i = 0; i < no_of_categories; i++) {
                JSONObject auditQuestionsArrayObject = auditQuestionsArray.getJSONObject(i);
                category = auditQuestionsArrayObject.getString("categoryName");


                Tab1 myFrag = Tab1.newInstance(category);
                listOfFragInstances.add(myFrag);

                adapter.addFrag(myFrag, category);

            }
            viewPager.setAdapter(adapter);


        } catch (JSONException e) {
            e.printStackTrace();
        }


    }

public void activity_saveAll()
{
    for (int i=0; i<listOfFragInstances.size(); i++)
    {
        listOfFragInstances.get(i).saveAnswers();
    }
}
@覆盖
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.audit\u form\u活动);
toolbar=(toolbar)findviewbyd(R.id.toolbar);
设置支持操作栏(工具栏);
tabLayout=(tabLayout)findViewById(R.id.tabLayout);
按钮=(按钮)findViewById(R.id.button);
toolbar=(toolbar)findviewbyd(R.id.toolbar);
设置支持操作栏(工具栏);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
viewPager=(viewPager)findViewById(R.id.viewPager);
设置viewPager(viewPager);
viewPager.setOffscreenPageLimit(没有类别);
tabLayout=(tabLayout)findViewById(R.id.tabLayout);
tabLayout.setupWithViewPager(viewPager);
}
专用无效设置ViewPager(ViewPager ViewPager){
//外环
ViewPagerAdapter适配器=新的ViewPagerAdapter(getSupportFragmentManager());
//从数据源获取JSON数据
试一试{
JSONObject pageObj=新JSONObject(第页);
JSONArray审核问题array=pageObj.getJSONArray(“审核问题”);
int auditQuestionsArray长度=auditQuestionsArray.length();
类别的数量=审核问题的总长度;
//通过CategoryName的外部循环为每个CategoryName创建片段。
for(int i=0;i对于(int i=0;i,您面临的问题是ViewPager处理片段的生命周期,这意味着您存储在ArrayList中的实例可能与ViewPager使用的实例不同

中的建议是不要保留片段的实例,让ViewPager完成它的工作。老实说,我从未接受过这种情况。要解决这个问题,您需要扩展FragmentManager,并在ViewPager内部更改时替换ArrayList中的实例

这是我用的

相关的代码是
public Object instanceItem(ViewGroup container,int position)
,当一个新片段被实例化时,它将替换ArrayList中的片段。这不是我的创作,我是从SO中的另一个问题中得到的,但不记得是哪一个

要获得正确的当前片段实例,请使用
公共片段getCurrentIntance(int位置)

这可能很明显,但以防万一,不要将对片段的引用保留在此类之外,因为您不知道它们是否是当前的

public class MySimplePagerAdapter extends FragmentStatePagerAdapter {

    protected final List<Fragment> fragmentList = new ArrayList<>();
    protected final List<String> fragmentTitleList = new ArrayList<>();
    protected final HashMap<Integer,Fragment> currentInstances = new HashMap<>();
    protected FragmentManager fm;

    public MySimplePagerAdapter(FragmentManager manager) {
        super(manager);
        this.fm = manager;
    }

    @Override
    public Fragment getItem(int position) {
        return fragmentList.get(position);

    }

    @Override
    public int getCount() {
        return fragmentList.size();
    }

    //Method to add Tab fragment and Tab name
    public void addFragment(Fragment fragment, String title) {
        fragmentList.add(fragment);
        fragmentTitleList.add(title);
    }

    @Override
    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return fragmentTitleList.get(position);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        Fragment createdFragment = (Fragment) super.instantiateItem(container, position);
        currentInstances.put(position, createdFragment);
        // save the appropriate reference depending on position
        return createdFragment;
    }

    public Fragment getCurrentIntance(int position){
        return currentInstances.get(position);
    }
}
公共类MySimplePagerAdapter扩展了FragmentStatePagerAdapter{
受保护的最终列表fragmentList=new ArrayList();
受保护的最终列表碎片标题列表=新的ArrayList();
受保护的最终HashMap currentInstances=新HashMap();
保护碎片管理器;
公共MySimplePagerAdapter(碎片管理器){
高级经理;
this.fm=经理;
}
@凌驾
公共片段getItem(int位置){
返回碎片列表。获取(位置);
}
@凌驾
public int getCount(){
返回fragmentList.size();
}
//方法添加选项卡片段和选项卡名称
public void addFragment(片段片段,字符串标题){
碎片列表。添加(碎片);
碎片标题列表。添加(标题);
}
@凌驾
public int getItemPosition(对象){
返回位置\无;
}
@凌驾
公共字符序列getPageTitle(int位置){
返回碎片标题列表。获取(位置);
}
@凌驾
公共对象实例化项(视图组容器,int位置){
Fragment createdFragment=(Fragment)super.instanceItem(容器,位置);
currentInstances.put(位置,createdFragment);
//根据位置保存适当的参考
返回createdFragment;
}
公共片段getCurrentIntance(内部位置){
返回currentInstances.get(位置);
}
}

是否
活动\u saveAll
是否在同一活动或片段中?这是您使用
列表碎片实例的唯一位置吗?它在同一活动中。我已尝试在片段中调用此方法。好的。是否可能在网络调用中获得数据之前从片段中调用它请告诉我们您是如何从片段中调用它的。