我如何在viewpager android中处理二维数组?

时间:2017-04-27 04:36:42

标签: android android-viewpager

我的考试试卷数据以下图所示的形式存储:

enter image description here

我正在尝试使用视图寻呼机按顺序显示此信息,但不知道该怎么做。我希望视图寻呼机在一个部分中显示所有问题,然后转到下一部分并在那里显示问题,依此类推,如果有下一部分的话。如果没有下一部分,论文就会结束。这将允许我制作具有不同部分和不同类型问题的试卷。我当前的查看分页器代码只是在一个部分显示问题,如下所示:

public class QuestionAdapter extends android.support.v13.app.FragmentStatePagerAdapter  {

    private  int NUM_ITEMS;

    public QuestionAdapter(FragmentManager fragmentManager, int num_items){
        super(fragmentManager);

        NUM_ITEMS = num_items;

    }

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



    @Override
    public Fragment getItem(int position){

        return  QuestionContainerFragment.newInstance(position);

    }

}

1 个答案:

答案 0 :(得分:1)

你可以试试这样的......

public class QuestionAdapter extends FragmentStatePagerAdapter  {

    private  int NUM_ITEMS;

    // this is the object in which you are storing your data 
    private YourData yourData;

    //when you initialize the adapter, send your data
    public QuestionAdapter(FragmentManager fragmentManager, int num_items, YourData yourData){
        super(fragmentManager);
        NUM_ITEMS = num_items;
        this.yourData = yourData;  
    }

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



    @Override
    public Fragment getItem(int position){
        //based on position, send appropriate data to your fragment   
        switch(position){
           case 0:   
             return  QuestionContainerFragment.newInstance(yourData.sectionOne);

           case 1:   
             return  QuestionContainerFragment.newInstance(yourData.sectionTwo);

           case 2:   
             return  QuestionContainerFragment.newInstance(yourData.sectionThree);
....
....
....
        }
    }

}

在QuestionContainerFragment中,解析并显示刚刚发送的数据。