Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/218.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_Retrofit2_Android Architecture Components - Fatal编程技术网

Android 如何在存储库类获取数据时通知活动?

Android 如何在存储库类获取数据时通知活动?,android,retrofit2,android-architecture-components,Android,Retrofit2,Android Architecture Components,我是android架构组件的新手。这是我的代码,我不知道如何通知我的活动并返回结果 这是我的密码: 活动: private void iniViewModels() { Observer<List<User>>usersObserver=new Observer<List<User>>() { @Override public void onChanged(@Nullable Lis

我是android架构组件的新手。这是我的代码,我不知道如何通知我的活动并返回结果

这是我的密码:

活动:

 private void iniViewModels() {
        Observer<List<User>>usersObserver=new Observer<List<User>>() {
            @Override
            public void onChanged(@Nullable List<User> users) {
                Log.v("this","LiveData: ");
                for (int i=0;i<users.size();i++){
                    Log.v("this",users.get(i).getName());
                }
            }
        };

        mViewModel = ViewModelProviders.of(this)//of-->name of act or fragment
                .get(AcActivityViewModel.class);///get -->the name of viewModelClass

        mViewModel.mUsers.observe(this,usersObserver);
    }
inviewModels()中的私有void{
ObserverusersObserver=新的观察者(){
@凌驾
公共void onChanged(@Nullable List users){
Log.v(“this”,“LiveData:”);
for(int i=0;动作或片段的名称)
.get(AcActivityViewModel.class);///get-->viewModelClass的名称
mViewModel.mUsers.observe(这是usersObserver);
}
这是我的viewModel类:

public class IpStaticViewModel extends AndroidViewModel {
public LiveData<List<Ipe>> ips;
private AppRepository repository;

public IpStaticViewModel(@NonNull Application application) {
    super(application);
    repository=AppRepository.getInstance(application.getApplicationContext());
}

public void getIpStatics() {
    repository.getStaticIps();
}
}
公共类IpStaticViewModel扩展了AndroidViewModel{
公共实时数据IP;
私人评估库;
公共IpStaticViewModel(@NonNull应用程序){
超级(应用);
repository=AppRepository.getInstance(application.getApplicationContext());
}
public void getIpStatics(){
getStaticIps();
}
}
这是我的存储库类:

public class AppRepository {
private static  AppRepository ourInstance ;
private Context context;
private IpStaticInterface ipInterface;

public static AppRepository getInstance(Context context) {
    if (ourInstance == null) {
        ourInstance=new AppRepository(context);
    }
    return ourInstance;
}

private AppRepository(Context context) {
    this.context=context;
}

public void getStaticIps() {
    ipInterface= ApiConnection.getClient().create(IpStaticInterface.class);
    ipInterface.getIpes()
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribeWith(new SingleObserver<IpStaticList>() {
                @Override
                public void onSubscribe(Disposable d) {

                }

                @Override
                public void onSuccess(IpStaticList ipStaticList) {
                    List<Ipe>ips=ipStaticList.getIpes();
                }

                @Override
                public void onError(Throwable e) {
                    Log.v("this","Eror "+ e.getMessage());
                }
            });
}
}
公共类附件{
私人静态评估机构;
私人语境;
私有IPStationInterface i接口;
公共静态AppRepository getInstance(上下文){
if(ourInstance==null){
ourInstance=新的感知(上下文);
}
回归自然状态;
}
私人评价者(上下文){
this.context=context;
}
public void getStaticIps(){
ipInterface=ApiConnection.getClient().create(ipstationInterface.class);
i接口.getIpes()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribowith(新的SingleObserver(){
@凌驾
认购的公共无效(一次性d){
}
@凌驾
成功时公共无效(IpStaticList IpStaticList){
Listips=ipStaticList.getIpes();
}
@凌驾
公共无效申报人(可丢弃的e){
Log.v(“this”、“Eror”+e.getMessage());
}
});
}
}
我正在使用翻新来获取数据,它成功地获取了数据,但我不知道如何通知我的活动

你能帮我吗?

有一个可变的LiveData

final MutableLiveData<List<Ipe>> data = new MutableLiveData<>();
final MutableLiveData data=new MutableLiveData();
由于

 public MutableLiveData<List<Ipe>> getStaticIps() {
   ipInterface= ApiConnection.getClient().create(IpStaticInterface.class);
   ipInterface.getIpes()
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribeWith(new SingleObserver<IpStaticList>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onSuccess(IpStaticList ipStaticList) {
                List<Ipe>ips=ipStaticList.getIpes();
                data.setValue(ips);
            }

            @Override
            public void onError(Throwable e) {
                Log.v("this","Eror "+ e.getMessage());
            }
        });
        return data;
  }
public MutableLiveData getStaticIps(){
ipInterface=ApiConnection.getClient().create(ipstationInterface.class);
i接口.getIpes()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribowith(新的SingleObserver(){
@凌驾
认购的公共无效(一次性d){
}
@凌驾
成功时公共无效(IpStaticList IpStaticList){
Listips=ipStaticList.getIpes();
数据设置值(ips);
}
@凌驾
公共无效申报人(可丢弃的e){
Log.v(“this”、“Eror”+e.getMessage());
}
});
返回数据;
}
在存储库中,将其公开给viewmodel

 public LiveData<List<Ipe>> getIpStatics() {
 return repository.getStaticIps();
 }
public LiveData getIpStatics(){
返回repository.getStaticIps();
}
在活动中,您观察livedata

 IpStaticViewModel viewmodel = ViewModelProviders.of(this
            .get(IpStaticViewModel.class)
  viewModel.getIpStatics().observe(this, new Observer<List<Ipe>>() {
        @Override
        public void onChanged(@Nullable List<Ipe> ipes) {
            if (ipes != null) {
                 // dosomething
            }
        }
    });
IpStaticViewModel viewmodel=ViewModelProviders.of(此
.get(IpStaticViewModel.class)
viewModel.getIpStatics().observe(这个,新的观察者(){
@凌驾
更改后的公共void(@Nullable List ipes){
如果(ipes!=null){
//剂量
}
}
});
如果你想概括你的回答,以防你有错误或什么,请看一看