Save spinner selected item to Android Room

Viewed 693

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

1 Answers

I am not sure why Your AsyncTask is not working and is causing infinite loop. I don't think You should be using it in the first place, especially not in 2018 when you have RxJava :)

First, add to your project RxJava and RxAndroid

implementation "io.reactivex.rxjava2:rxjava:2.1.13"
implementation "io.reactivex.rxjava2:rxandroid:2.0.2"

Your ViewModel code

public void updateQuantity(CartItem cartItem, int quantity){
      val updatedCartItem = //todo update cartItem quantity here
      addDisposable(cartRepository.updateQuantity(updatedCartItem))
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .doOnError { handleFailure(it) }
            .subscribe { processResponse(it) })
}

private val compositeDisposable: CompositeDisposable = CompositeDisposable()

fun addDisposable(disposable: Disposable) {
    compositeDisposable.add(disposable)
}

Your Repository code

fun updateQuantity(cartItem: CartItem): Single<Int> {
    return mAsyncCartDao.updateQuantity(cartItem)
}

Your Dao code

interface Dao {

@Update(onConflict = OnConflictStrategy.REPLACE)
     fun updateQuantity(item: CartItem): Single<Int>

}

I wrote this code in kotlin cause I am not using Java anymore in android development. I hope this will help You.

Related