简体   繁体   中英

Adding Values to Model Array Object from Adapter

I have a Specialty[] array that is an object of a model class in my adapter. I want to clear the Specialty[] array before adding new values. For that purpose re-initialize it. When trying to add a new value I am getting index out of range. Is there any way we can increase the size of the model object? Thanks. Here is my code:

  //reinitializing 
  mgm = new Specialties []{};
  mgm[0].setName("abc");
  • You can use java.util.Arrays.copyOf(...) to resize
  • Or Replace by ArrayList
  • Or Before add new value, reinitial mgm = new Specialties [1];

Yes, you got that issue. Because you cannot change your Array size after initializing it.

 mgm = new Specialties []{};  -> you create an Array with size = 0. You cannot change it later.

I guess that you should use ArrayList instead.

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