将微调器选择的项目保存到 Android 房间

Save spinner selected item to Android Room

我正在尝试将微调器 selection 保存到 Android 房间数据库,但每次我 运行 代码时,微调器 onSelect 侦听器都会陷入无限环形。旋转器是卡片的一部分,由回收器视图生成。每张卡片都无法被点击,并且微调器无法更改其值,因为它在此循环中处于 运行ning 状态。我只是想 select 来自微调器的一个项目,然后在数据库中更新它。

旋转监听器

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) {

        }
    });

存储库代码

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 代码

@Query("UPDATE CART_TABLE SET quantity = :quan WHERE itemID = :id ")
void updateQuantity(int id, int quan);

查看模型代码

public void updateQuantity(CartItem cartItem, int quantity){
    cartRepository.updateQuantity(cartItem,quantity);
}

微调器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" />

很高兴提供更多信息! 感谢任何帮助!

干杯

我不确定为什么您的 AsyncTask 无法正常工作并导致无限循环。我不认为你首先应该使用它,尤其是在 2018 年你有 RxJava :)

首先,将 RxJava 和 RxAndroid

添加到您的项目中
implementation "io.reactivex.rxjava2:rxjava:2.1.13"
implementation "io.reactivex.rxjava2:rxandroid:2.0.2"

您的 ViewModel 代码

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)
}

您的 Repository 代码

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

您的 Dao 代码

interface Dao {

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

}

我在 kotlin 中编写了这段代码,因为我在 android 开发中不再使用 Java。希望对您有所帮助。