如何声明一个由列表项gloabal组成的数组,可以在以后初始化?

时间:2017-01-11 08:20:54

标签: android spinner

我有两个列表项数组。我想根据某些条件将其项目发送到适配器。所以我多次初始化它并对它应用条件,如果为true则将该项数量发送到适配器。

以下是更好理解的代码

int images[];
    String levels[];

  @Override
       protected void onCreate(@Nullable Bundle savedInstanceState) {


        super.onCreate(savedInstanceState);

        setContentView(R.layout.cannon_spinner_numbers);


//// initialize all your visual fields
//          if (savedInstanceState != null) {
//              sp1.setSelection(savedInstanceState.getInt("MySpinner1", 0));
//              // do this for each of your text views
//          }

          if (bundle.getString("classFrom").equals(TownHallNineActivity.class.toString())) {

           images[] = {R.drawable.cannon1,
                      R.drawable.cannon2,
                      R.drawable.cannon3,
                      R.drawable.cannon4,
                      R.drawable.cannon5,
                      R.drawable.cannon6,
                      R.drawable.cannon7,
                      R.drawable.cannon8,
                      R.drawable.cannon9,
                      R.drawable.cannon10,
                      R.drawable.cannon11,
                      R.drawable.cannon12,
                      R.drawable.cannon13,
                      R.drawable.cannon14
              };

              String levels[] = {"Level 1",
                      "Level 2",
                      "Level 3",
                      "Level 4",
                      "Level 5",
                      "Level 6",
                      "Level 7",
                      "Level 8",
                      "Level 9",
                      "Level 10",
                      "Level 11",
                      "Level 12",
                      "Level 13",
                      "Level 14",};

          }

if (bundle.getString("classFrom").equals(TownHallTenActivity.class.toString())) {

           images[] = {R.drawable.cannon1,
                      R.drawable.cannon2,
                      R.drawable.cannon3,
                      R.drawable.cannon4,
                      R.drawable.cannon5,
                      R.drawable.cannon6,
                      R.drawable.cannon7,
                      R.drawable.cannon8,
                      R.drawable.cannon9,
                      R.drawable.cannon10,
                      R.drawable.cannon11,
                      R.drawable.cannon12,

              };

              String levels[] = {"Level 1",
                      "Level 2",
                      "Level 3",
                      "Level 4",
                      "Level 5",
                      "Level 6",
                      "Level 7",
                      "Level 8",
                      "Level 9",
                      "Level 10",
                      "Level 11",
                      "Level 12"
                      };

          }

然后将theese项设置为Adapter

 SpinnerAdapter adapter=new SpinnerAdapter(this,levels,images);



          sp1.setAdapter(adapter);

初始化ImageArray / String Array时出现此错误

enter image description here

它表示意外的令牌。另外这是通过仅将那些项目发送到要显示的适配器来隐藏旋转器中的项目的正确方法吗?

1 个答案:

答案 0 :(得分:0)

你似乎在不需要它们的地方有逗号:在"等级14"之后" R.drawable.cannon12"。这就是"意外的令牌"。

我建议使用一个ArrayList,其元素最多为12(两者都共享),然后使用 .add(element)在相应的if语句中添加下两个元素。