需要在MainActivity中等待改造请求的答案

时间:2019-05-18 05:53:23

标签: java android retrofit

我有翻新请求,其中返回为我的dataSet字段分配的List。我在onResponse方法中做到了。但是在MainActivity中初始化回收视图比请求更快,并且视图什么也没显示。我该如何等待onResponse方法。

我在NewsRepository.java类中有这两种方法

 private void setNews(){
    GetDataService service = RetrofitClientInstance.getRetrofitInstance().create(GetDataService.class);
    Call<ItemsAPI> call = service.getAllItems();
    call.enqueue(new Callback<ItemsAPI>() {
        @Override
        public void onResponse(Call<ItemsAPI> call, Response<ItemsAPI> response) {
            Log.d(TAG, "onResponse");
            items = response.body();
            dataSet = items.getItems();
            Log.d(TAG, dataSet.get(0).getTitle());
        }

        @Override
        public void onFailure(Call<ItemsAPI> call, Throwable t) {
            Log.d(TAG, "onFailure "+ t.getMessage());
        }
    });
}
public MutableLiveData<List<News>> getNews(){
    setNews();
    MutableLiveData<List<News>> data = new MutableLiveData<>();
    data.setValue(dataSet);
    return data;
}

并在MainActivityModelView.java中拥有它

  public void init(){
    if(mNews != null){
        return;
    }
    mRepo = NewsRepository.getInstance();
    mNews = mRepo.getNews();
}

这是MyActivity.java

public class MainActivity extends AppCompatActivity {

private static final String TAG = "MainActivity";

private RecyclerView mRecycleView;
private RecycleViewAdapter mAdapter;
private MainActivityViewModel mMainActivityViewModel;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mRecycleView = findViewById(R.id.recyclev_view);

    mMainActivityViewModel = ViewModelProviders.of(this).get(MainActivityViewModel.class);

    mMainActivityViewModel.init();

    mMainActivityViewModel.getNews().observe((LifecycleOwner) this, new Observer<List<News>>() {
        @Override
        public void onChanged(@Nullable List<News> news) {
            mAdapter.notifyDataSetChanged();
        }
    });

    mMainActivityViewModel.getIsUpdating().observe((LifecycleOwner) this, new Observer<Boolean>() {
        @Override
        public void onChanged(@Nullable Boolean aBoolean) {
            if(aBoolean){
            }
            else{
                mRecycleView.smoothScrollToPosition(mMainActivityViewModel.getNews().getValue().size()-1);
            }
        }
    });

    initRecyclerView();
    Log.d(TAG, "RecycleView is inited");
}

private void initRecyclerView(){
    mAdapter = new RecycleViewAdapter(this, mMainActivityViewModel.getNews().getValue());
    RecyclerView.LayoutManager linearLayoutManager = new LinearLayoutManager(this);
    mRecycleView.setLayoutManager(linearLayoutManager);
    mRecycleView.setAdapter(mAdapter);
}

}

活动初始化比我的请求快。

2019-05-18 09:32:12.299 13859-13859/com.krasnov.rxjavalearning D/MainActivity: RecycleView is inited
2019-05-18 09:32:13.098 13859-13859/com.krasnov.rxjavalearning D/NewsRepository: onResponse

3 个答案:

答案 0 :(得分:0)

在onResponse中,您应该通知适配器添加以下内容:

        yourAdapter.notifyDataSetChanged();

答案 1 :(得分:0)

我认为最好将此代码移到findViewById下的其他代码之上

getDisplayedValues

在此功能中,最好不要使用此承包商

initRecyclerView();
Log.d(TAG, "RecycleView is inited");

改为使用它(创建承包商)

mAdapter = new RecycleViewAdapter(this, mMainActivityViewModel.getNews().getValue());

当您的数据到达时,在适配器中触发此功能

 mAdapter = new RecycleViewAdapter(this);

所以您的代码就像

 public void setData( List<News> list) {
    this.list= list
    notifyDataSetChanged()
}

答案 2 :(得分:0)

  

在简历上致电改造服务,   响应后创建一个回调   通知后,ViewModel中的方法在Activty类中进行通知   初始化recyclerview。