我正在尝试在onCreate()中设置RecyclerView的适配器。我读到只能在主线程上调用notifyDatasetChanged()。但是如何实现呢?到目前为止,这是我的代码:
RecyclerView recyclerViewSt;
List<GitHubstarredRepos> myDataSource = new ArrayList<>();
RecyclerView.Adapter myAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
recyclerViewSt = (RecyclerView) findViewById(R.id.starred_repos_recycler_view);
recyclerViewSt.setLayoutManager(new LinearLayoutManager(this));
myAdapter = new StarredReposAdapter(myDataSource, R.layout.list_item_starred_repo,
getApplicationContext());
recyclerViewSt.setAdapter(myAdapter);
}
public void loadStarredRepos (View view){
GitHubStarredRepoAPI apiService =
ApiClient.getClient().create(GitHubStarredRepoAPI.class);
Call<List<GitHubstarredRepos>> call = apiService.getStarredRepoName(newString);
call.enqueue(new Callback<List<GitHubstarredRepos>>() {
@Override
public void onResponse(Call<List<GitHubstarredRepos>> call, Response<List<GitHubstarredRepos>>
response) {
myDataSource.clear();
myDataSource.addAll(response.body());
recyclerViewSt.notify();
}
谢谢!
答案 0 :(得分:1)
而不是通知RecyclerView
,通知您的Adapter
。