I am trying to save a spinner selection to an Android Room Database, but every time I run the code, the spinner onSelect listener keeps getting stuck in an infinite loop. The spinner is apart of a card, generated by a recycler view. Each card is unable to be clicked and the spinner cannot change its value as it is running in this loop. I am simply trying to select an item from the spinner and then update it in the database.
SPINNER LISTENER
holder.itemQuantity.setOnItemSelectedListener(new
AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int spinnerPosition, long id) {
cartViewModel.updateQuantity(cartItems.get(position), holder.itemQuantity.getSelectedItemPosition() + 1);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
REPOSITORY CODE
public void updateQuantity(CartItem cartItem, int quan){
new updateQuantityAsyncTask(mCartItemDao, quan).execute(cartItem);
}
private static class updateQuantityAsyncTask extends AsyncTask<CartItem, Void, Void>{
private CartItemDao mAsyncCartDao;
private int quan;
updateQuantityAsyncTask(CartItemDao cartItemDao, int quan){
mAsyncCartDao = cartItemDao;
this.quan = quan;
}
@Override
protected Void doInBackground(final CartItem... params){
mAsyncCartDao.updateQuantity(params[0].getItemID(),quan);
return null;
}
}
DAO CODE
@Query("UPDATE CART_TABLE SET quantity = :quan WHERE itemID = :id ")
void updateQuantity(int id, int quan);
VIEW MODEL CODE
public void updateQuantity(CartItem cartItem, int quantity){
cartRepository.updateQuantity(cartItem,quantity);
}
SPINNER XML
<Spinner
android:id="@+id/cartCardSpinner"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@+id/guideline13"
app:layout_constraintTop_toTopOf="@+id/guideline15" />
Happy to provide more info! Appreciate any help!
Cheers