gpt4 book ai didi

java - Rxjava,使用.zip合并两个livedata对象

转载 作者:行者123 更新时间:2023-12-01 18:00:28 27 4
gpt4 key购买 nike

我正在尝试使用 rxjava .zip 合并两个 livedata 对象。这就是我到目前为止所拥有的。

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.util.Consumer;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;


import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.example.myapplication.API.model.OrderRows;
import com.example.myapplication.API.model.Orders;


import java.util.function.BiFunction;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button myButt;
private MainViewModel mvw;
private TextView myView;


@RequiresApi(api = Build.VERSION_CODES.N)
@Override
protected void onCreate(Bundle savedInstanceState) {
mvw = new ViewModelProvider(this).get(MainViewModel.class);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myButt = findViewById(R.id.button);
myView = findViewById(R.id.textview);
myButt.setOnClickListener(this);
final Observer<Orders> obsOrders = new Observer<Orders>() {
@Override
public void onChanged(Orders orders) {
Log.i("order obs", "order obs");
}
};
final Observer<OrderRows> obsOrderRow = new Observer<OrderRows>() {
@Override
public void onChanged(OrderRows orders) {
Log.i("orderRows obs", "orderRow obs");
}
};
mvw.getAllOrders().observe(this,obsOrders);
mvw.getAllOrderRows().observe(this,obsOrderRow);

Observable.zip(obsOrderRow,obsOrders,mergeItems())
.subscribe(printer());


}
@RequiresApi(api = Build.VERSION_CODES.N)
@NonNull
private BiFunction<OrderRows, Orders, String> mergeItems() {
return new BiFunction<OrderRows, Orders, String>() {
@Override
public String apply(OrderRows orderRows, Orders orders) {
return "asd";
}
};
}
@NonNull
private Consumer<String> printer() {
return new Consumer<String>() {
@Override
public void accept(String s) {
Log.i("merged",s);
}
};
}


@Override
public void onClick(View view) {
switch(view.getId()){
case R.id.button:
Log.i("button","clicked");
mvw.updateOrderData(); // calls for new values to be fetched
mvw.updateOrderRowData();
}
}
}

问题出在 .zip 函数上,当我尝试运行此函数时,出现以下错误。

error: no suitable method found for zip(Observer,Observer,BiFunction) Observable.zip(obsOrderRow,obsOrders,mergeItems()) ^

还添加了导入,以便大家可以看到。

最佳答案

您使用了错误的BiFunction

你有:

import java.util.function.BiFunction;

同时zip需要io.reactivex.functions.BiFunction .

关于java - Rxjava,使用.zip合并两个livedata对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60643382/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com