Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/230.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将微调器所选项目保存到Android房间_Android_Android Spinner_Android Room_Selecteditem - Fatal编程技术网

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

将微调器所选项目保存到Android房间,android,android-spinner,android-room,selecteditem,Android,Android Spinner,Android Room,Selecteditem,我试图将微调器选择保存到Android Room数据库中,但每次运行代码时,微调器onSelect侦听器都会陷入无限循环。微调器是卡的一部分,由回收器视图生成。无法单击每张卡,并且微调器无法更改其值,因为它正在该循环中运行。我只是尝试从微调器中选择一个项目,然后在数据库中更新它 微调器侦听器 holder.itemQuantity.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Over

我试图将微调器选择保存到Android Room数据库中,但每次运行代码时,微调器onSelect侦听器都会陷入无限循环。微调器是卡的一部分,由回收器视图生成。无法单击每张卡,并且微调器无法更改其值,因为它正在该循环中运行。我只是尝试从微调器中选择一个项目,然后在数据库中更新它

微调器侦听器

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 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" />

很高兴提供更多信息! 谢谢你的帮助


干杯

我不确定为什么您的
异步任务
不起作用,导致无限循环。我认为你不应该一开始就使用它,尤其是在2018年,当你有了RxJava:)

首先,将RxJava和RxAndroid添加到项目中

implementation "io.reactivex.rxjava2:rxjava:2.1.13"
implementation "io.reactivex.rxjava2:rxandroid:2.0.2"
您的
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)
}
fun updateQuantity(cartItem: CartItem): Single<Int> {
    return mAsyncCartDao.updateQuantity(cartItem)
}
interface Dao {

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

}
您的
存储库
代码

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)
}
fun updateQuantity(cartItem: CartItem): Single<Int> {
    return mAsyncCartDao.updateQuantity(cartItem)
}
interface Dao {

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

}

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

谢谢您的回复,伙计!请详细说明您编写的存储库代码。特别是,此代码是否位于异步任务“updateQuantityAsyncTask()”中?如何访问“params”,而不使用“doInBackground”方法?我正在努力将Kotlin转换为Java。干杯我的错误,您应该从方法参数传递cartItem,我忘记更改这个,我已经编辑了我的代码。