在自定义适配器中保留项目时ArrayIndexOutOfBoundsException

时间:2017-01-11 19:10:01

标签: java android spinner android-spinner indexoutofboundsexception

相同的适配器在加载12-14项时工作正常,但在加载少于10项时会出错。

当我输入6个项目时导致错误= ArrayIndexOutOfBoundsException:length = 6; index = 6

public class SpinnerAdapterAirSweeper extends ArrayAdapter <String>{


    Context c;

    String[] levels;
    int[] images;

    public SpinnerAdapterAirSweeper(Context ctx, String[] levels, int[] images){

        super(ctx,R.layout.spinner_building_to_upgrade,levels);
        this.c=ctx;
        this.images=images;
        this.levels=levels;


    }

    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {



        if(convertView==null){

            LayoutInflater inflater=(LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView=inflater.inflate(R.layout.spinner_building_to_upgrade,null);
        }

        TextView tx=(TextView) convertView.findViewById(R.id.levelNumber);
        ImageView image=(ImageView)convertView.findViewById(R.id.imageid);

        //SET DATA
      //  if (levels.length > position) {
            tx.setText(levels[position]);
     //   }
     //   if (images.length > position) {
            image.setImageResource(images[position]);
    //    }
//        tx.setText(levels[position]);
//        image.setImageResource(images[position]);




        return convertView;
    }

    @NonNull
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if(convertView==null){

            LayoutInflater inflater=(LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView=inflater.inflate(R.layout.spinner_building_to_upgrade,null);
        }

        TextView tx=(TextView) convertView.findViewById(R.id.levelNumber);
        ImageView image=(ImageView)convertView.findViewById(R.id.imageid);

        //SET DATA

      //  if (levels.length > position) {
            tx.setText(levels[position]);
     //   }
     //   if (images.length > position) {
            image.setImageResource(images[position]);
     //   }
//        tx.setText(levels[position]);
//        image.setImageResource(images[position]);



        return convertView;
    }

    @Override
    public int getItemViewType(int position) {



        return super.getItemViewType(position);
    }
}

请向我提供解决方案,并附上修正此代码的代码。早先这个适配器工作正常。感谢

编辑: 这就是我将值传递给适配器

的方法
public class AirSweeperActivity extends AppCompatActivity {


    static   int airSweeperLevelOne=22500;
    static  int airSweeperLevelTwo=90000;
    static   int airSweeperLevelThree=270000;
    static   int airSweeperLevelFour=540000;
    static  int airSweeperLevelFive=1080000;
    static  int airSweeperLevelSix=2160000;


    int sp1AirSweeperUpgradeCost;
    int sp2AirSweeperUpgradeCost;

    long totalAirSweeperUpgradeCost;


    Spinner sp1;
    Spinner sp2;

    TextView tx;
    Button sbmt;
    String levels[]={"Level 1",
            "Level 2",
            "Level 3",
            "Level 4",
            "Level 5",
            "Level 6",

            };
    int[] images={
            R.drawable.air_sweeper1,
            R.drawable.air_sweeper2,
            R.drawable.air_sweeper3,
            R.drawable.air_sweeper4,
            R.drawable.air_sweeper5,
            R.drawable.air_sweeper6,

    };



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

        sbmt = (Button) findViewById(R.id.submit);
        tx = (TextView) findViewById(R.id.upgradeResult);
        sp1 = (Spinner) findViewById(R.id.spinner);
        sp2 = (Spinner) findViewById(R.id.spinner1);

        SpinnerAdapterAirSweeper adapter = new SpinnerAdapterAirSweeper(this, levels, images);
        sp1.setAdapter(adapter);
        sp2.setAdapter(adapter);


            sp1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                    SharedPreferences sharedPreferences = getSharedPreferences("your_prefs", Activity.MODE_PRIVATE);
                    SharedPreferences.Editor editor = sharedPreferences.edit();


                    prefs.edit().putInt("spinner1_indx", sp1.getSelectedItemPosition()).apply(); //TO RETRIEVE SPINNER 1 ON RESUME

                    switch (position) {

                        case 0:


                            sp1AirSweeperUpgradeCost = (airSweeperLevelTwo + airSweeperLevelThree + airSweeperLevelFour + airSweeperLevelFive +
                                    airSweeperLevelSix );

                            editor.putInt("sp1_Air_Defence", sp1AirSweeperUpgradeCost);
                            editor.apply();


                            break;
                        case 1:


                            sp1AirSweeperUpgradeCost = (airSweeperLevelThree + airSweeperLevelFour + airSweeperLevelFive +
                                    airSweeperLevelSix );

                            editor.putInt("sp1_Air_Defence", sp1AirSweeperUpgradeCost);
                            editor.apply();
                            break;

                        case 2:

                            sp1AirSweeperUpgradeCost = (airSweeperLevelFour + airSweeperLevelFive +
                                    airSweeperLevelSix);
                            editor.putInt("sp1_Air_Defence", sp1AirSweeperUpgradeCost);
                            editor.apply();

                            break;


                        case 3:

                            sp1AirSweeperUpgradeCost = (airSweeperLevelFive +
                                    airSweeperLevelSix );
                            editor.putInt("sp1_Air_Defence", sp1AirSweeperUpgradeCost);
                            editor.apply();
                            break;

                        case 4:

                            sp1AirSweeperUpgradeCost = (
                                    airSweeperLevelSix );
                            editor.putInt("sp1_Air_Defence", sp1AirSweeperUpgradeCost);
                            editor.apply();
                            break;


                        case 5:

                            sp1AirSweeperUpgradeCost = (0);
                            editor.putInt("sp1_Air_Defence", sp1AirSweeperUpgradeCost);
                            editor.apply();
                            break;


                    }

                }

                @Override
                public void onNothingSelected(AdapterView<?> parent) {

                }
            });

1 个答案:

答案 0 :(得分:0)

这不是关于价值的尝试。你在这里添加了额外的逗号

String levels[]={"Level 1",
            "Level 2",
            "Level 3",
            "Level 4",
            "Level 5",
            "Level 6"

            };
    int[] images={
            R.drawable.air_sweeper1,
            R.drawable.air_sweeper2,
            R.drawable.air_sweeper3,
            R.drawable.air_sweeper4,
            R.drawable.air_sweeper5,
            R.drawable.air_sweeper6

    };