user6456773 user6456773 - 2 months ago 26
Android Question

java.lang.IllegalMonitorStateException: object not locked by thread before notify() - onResponse

I am trying to set the adapter of a RecyclerView within the onCreate(). I read that the notifyDatasetChanged() can be invoked on the main thread only. But how can this be achieved? Here is my code so far:

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

}


Thank you!

Answer

instead of notifying RecyclerView, Notify your Adapter.