I am trying to edit an element in an already populated arraylist but keep getting that error. The method is called from a fragment but I can't seem to access the arraylist from the fragment or Anything the fragment is associated with. I can change the value I want anywhere else in the same class expect in that method which is called from the fragment. I will really appreciate the help thanks in advance.
Fragment Code
MainActivity method = new MainActivity();
@Override
public void onDetach() {
method.updateQuantity(Quantity);
super.onDetach();
}
MainActivity code
public void updateQuantity(int Quantity){
items.get(currentitem).setQuantity(Quantity);
}
If items
is your ArrayList, then for ArrayList.get
, it takes integer(index)
as an argument and then returns the element of that index.
So, this items.get(currentitem).setQuantity(Quantity)
) call is very unsafe. Need to check the max index length first then try to access it.
public void updateQuantity(int Quantity){
if(items != null and currentitem < items.size(){
items.get(currentitem).setQuantity(Quantity);
}
}
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.