在pageData

时间:2018-11-22 15:52:40

标签: android arrays android-intent dynamic-variables

我想使用意图调用相同的ViewPagerActivity,但是作为活动的pageData的源,我想根据活动使用不同的数组,从而调用视图传呼机。所以,我尝试了这个

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_viewpager);
    //Get the data to be swiped through

    Intent intent = getIntent();
    String ArrayName = intent.getStringExtra("ArrayName");

    //pageData=getResources().getStringArray(R.array.numbers);
    pageData=getResources().getStringArray(R.array.ArrayName);
    //get an inflater to be used to create single pages
    inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    //Reference ViewPager defined in activity
    vp=(ViewPager)findViewById(R.id.viewPager);
    //set the adapter that will create the individual pages
    vp.setAdapter(new MyPagesAdapter());

我在开始活动“数字”中

intent.putExtra("ArrayName", "numbers"); startActivity(intent);

,并且如果我从“数字”活动但在此行pageData=getResources().getStringArray(R.array.ArrayName)的ViewPagerActivity上调用视图传呼器,则想获取“ R.array.numbers”而不是“ R.array.ArrayName”

我有一个错误“错误:找不到符号变量ArrayName “。如何避免此错误?

1 个答案:

答案 0 :(得分:0)

使用开关或其他结构来实现此目的

           String Arrayname = getIntent().getStringExtra("ArrayName");
            switch(Arrayname.toLowerCase()){
                case "numbers":
                pageData=getResources().getStringArray(R.array.numbers);
                mAdapter = new MyPagesAdapter(pageDate);
                    break;
                case "alphabet":
                list=getResources().getStringArray(R.array.alphabet);
                mAdapter = new MyPagesAdapter(list)
                    break;

            }
          vp.setAdapter(mAdapter);

等等,这是概述而不是确切的代码