简体   繁体   中英

Efficiently add multiple items to arraylist object to avoid out of bounds exception when sending intent to another activity in android studio java

I'm trying to find a way to efficiently add multiple add statements to an array list of an object.

For example:

In my MainActivity (cut down version) I'm adding to the array of a car object there's quite a few:

public class MainActivity extends AppCompatActivity {

   ArrayList<Car> imagesArray = new ArrayList<>();


   @Override
   protected void onCreate(Bundle savedInstanceState) {

    imagesArray.add(new Car(0,"alpha", "", R.mipmap.alpha));
    imagesArray.add(new Car(1,"audi", "", R.mipmap.audi));
    imagesArray.add(new Car(2,"bentley", "", R.mipmap.bentley));
    imagesArray.add(new Car(3,"bmw", "", R.mipmap.bmw));
    imagesArray.add(new Car(4,"bugatti", "", R.mipmap.bugatti));
    imagesArray.add(new Car(5,"ferrari", "", R.mipmap.ferrari));
    imagesArray.add(new Car(6,"ford", "", R.mipmap.ford));
    imagesArray.add(new Car(7,"honda", "", R.mipmap.honda));
    imagesArray.add(new Car(8,"hyundai", "", R.mipmap.hyundai));
    imagesArray.add(new Car(9,"jaguar", "", R.mipmap.jaguar));
    imagesArray.add(new Car(10,"jeep", "", R.mipmap.jeep));
    imagesArray.add(new Car(11,"lamborghini", "", R.mipmap.lamborghini));
    imagesArray.add(new Car(12,"maserati", "", R.mipmap.maserati));
    imagesArray.add(new Car(13,"mazda", "", R.mipmap.mazda));
    imagesArray.add(new Car(14,"mercedes", "", R.mipmap.mercedes));
    imagesArray.add(new Car(15,"mini", "", R.mipmap.mini));
    imagesArray.add(new Car(16,"mustang", "", R.mipmap.mustang));
    imagesArray.add(new Car(17,"nissan", "", R.mipmap.nissan));
    imagesArray.add(new Car(18,"pagani", "", R.mipmap.pagani));
    imagesArray.add(new Car(19,"porsche", "", R.mipmap.porsche));
    imagesArray.add(new Car(20,"rangerover", "", R.mipmap.rangerover));
    imagesArray.add(new Car(21,"renault", "", R.mipmap.renault));
    imagesArray.add(new Car(22,"rollsroyce", "", R.mipmap.rollsroyce));
    imagesArray.add(new Car(23,"seat", "", R.mipmap.seat));
    imagesArray.add(new Car(24,"skoda", "", R.mipmap.skoda));
    imagesArray.add(new Car(25,"subaru", "", R.mipmap.subaru));
    imagesArray.add(new Car(26,"suzuki", "", R.mipmap.suzuki));
    imagesArray.add(new Car(27,"tesla", "", R.mipmap.tesla));
    imagesArray.add(new Car(29,"toyota", "", R.mipmap.toyota));
    imagesArray.add(new Car(30,"volvo", "", R.mipmap.volvo));
    imagesArray.add(new Car(31,"vw", "", R.mipmap.vw));

    Button btnHints = (Button) findViewById(R.id.hints);


    btnHints.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            Log.i("Home Screen","Hints Clicked.");
            openHintsActivity();

        }
    });

 }

   public void openHintsActivity() {

    Intent intent = new Intent(this, Hints.class);
    intent.putExtra("imagesArray", imagesArray);
    startActivity(intent);

  }

Here's the car object:

public class Car implements Serializable {

int id;
String make;
String model;
int mipmap;

public Car(int id,String make, String model, int mipmap) {
    this.id = id;
    this.make = make;
    this.model = model;
    this.mipmap = mipmap;
}

//Getters and setters etc. below

The issue is SOMETIMES I get an out of bounds exception whenever I pass the intent over to another activity, I'm guessing this might be to do with the size of the array?

I have a feeling that sometimes this happens because when the application gets started not all the images are added before a user goes onto another activity so the entire intent of imagesArray isn't, complete?

As imagesArray is used in other functions in the application.

So my question is how is it best to efficiently to get around this out of bounds issue when adding a large amounts of data to an array list to then be passed as an intent? Is there a better solution?

Thanks for your advice.

Array is used here:

public class Hints extends AppCompatActivity {

StringBuilder temp;
int noOfAttempts = 0;
ArrayList<Car> imagesArray;


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

    imagesArray = (ArrayList<Car>)getIntent().getSerializableExtra("imagesArray");

    Collections.shuffle(imagesArray); //Randomizes array

    ImageView imageView = (ImageView) findViewById(R.id.imgRandom);

    final int randomMake = imagesArray.get(0).getId();
    imageView.setImageResource(imagesArray.get(randomMake).getMipmap());

I'm not sure if this will work, but you might be able to use an if statement to check if the randomMake variable is greater than the array length, and then just use the array length as the index. You could also use a try catch block for the indexOutOfBounds exception, and then just use the array length as the index.

*I know it's called an arrayList, but array sounds better when I read it.

In Your Hints Activity Change

    imagesArray = (ArrayList<Car>)getIntent().getSerializableExtra("imagesArray");

    Collections.shuffle(imagesArray); //Randomizes array

    ImageView imageView = (ImageView) findViewById(R.id.imgRandom);

    final int randomMake = imagesArray.get(0).getId();
    imageView.setImageResource(imagesArray.get(randomMake).getMipmap());

To

imagesArray = (ArrayList<Car>)getIntent().getSerializableExtra("imagesArray");
Random random = new Randome();
final int randomMake = random.nextInt(imagesArray.size());
imageView.setImageResource(imagesArray.get(randomMake).getMipmap());

To Make Sure You Are Not Getting A Number Higher Than The Array

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM