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

时间:2018-09-23 14:56:23

标签: android retrofit2 android-architecture-components

我是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);
    }

这是我的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();
}
}

这是我的存储库类:

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

我正在使用改造来获取数据,它成功获取了数据,但是我不知道如何通知我的活动

你能帮我吗?

1 个答案:

答案 0 :(得分:1)

具有MutableLiveData

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

在onSucess

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

在存储库中将其公开给viewmodel

 public LiveData<List<Ipe>> getIpStatics() {
 return repository.getStaticIps();
 }

在“活动”中,您观察实时数据

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

如果您想归纳总结以防万一您有任何错误或疑问,请查看https://github.com/googlesamples/android-architecture-components/blob/master/GithubBrowserSample/app/src/main/java/com/android/example/github/vo/Resource.kt