Answer the question
In order to leave comments, you need to log in
Cannot assign value to LoaderManager in onCreate method. And the error in onCreateLoader when returning a value?
Help with the problem, please. I am writing an app in android studio. The bottom line is this: it is not possible to assign a value to LoaderManager using the implementation of the Singletone pattern in the onCreate method. And also an error occurs when returning a value in the onCreateLoader method. All MainActivity code
public class MainActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks<JSONObject> {
private Switch switchSort;
private RecyclerView recyclerViewPosters;
private MovieAdapter movieAdapter;
private TextView textViewTopRated;
private TextView textViewPopularity;
private MainViewModel viewModel;
private static final int LOADER_ID = 133;
private LoaderManager loaderManager;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.itemMain:
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
break;
case R.id.itemFavourite:
Intent intentToFavourite = new Intent(this, FavoriteActivity.class);
startActivity(intentToFavourite);
break;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
loaderManager = LoaderManager.getInstance(this);
viewModel = ViewModelProviders.of(this).get(MainViewModel.class);
switchSort = findViewById(R.id.switchSort);
textViewTopRated = findViewById(R.id.textViewTopRated);
textViewPopularity = findViewById(R.id.textViewPopularity);
recyclerViewPosters = findViewById(R.id.recyclerviewPosters);
recyclerViewPosters.setLayoutManager(new GridLayoutManager(this, 2));
movieAdapter = new MovieAdapter();
recyclerViewPosters.setAdapter(movieAdapter);
switchSort.setChecked(true);
switchSort.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
setMethodOfSort(isChecked);
}
});
switchSort.setChecked(false);
movieAdapter.setOnPosterClickListener(new MovieAdapter.OnPosterClickListener() {
@Override
public void onPosterClick(int position) {
Movie movie = movieAdapter.getMovies().get(position);
Intent intent = new Intent(MainActivity.this, DetailActivity.class);
intent.putExtra("id", movie.getId());
startActivity(intent);
}
});
movieAdapter.setOnReachAndListener(new MovieAdapter.OnReachAndListener() {
@Override
public void onRichEnd() {
Toast.makeText(MainActivity.this, "Конец списка", Toast.LENGTH_SHORT).show();
}
});
LiveData<List<Movie>> moviesFromLiveData = viewModel.getMovies();
moviesFromLiveData.observe(this, new Observer<List<Movie>>() {
@Override
public void onChanged(List<Movie> movies) {
movieAdapter.setMovies(movies);
}
});
}
public void onClickSetPopularity(View view) {
setMethodOfSort(false);
switchSort.setChecked(false);
}
public void onClickSetTopRated(View view) {
setMethodOfSort(true);
switchSort.setChecked(true);
}
private void setMethodOfSort(boolean isTopRated) {
int methodOfSort;
if (isTopRated) {
textViewTopRated.setTextColor(getResources().getColor(R.color.teal_200));
textViewPopularity.setTextColor(getResources().getColor(R.color.white_color));
methodOfSort = NetworkUtils.TOP_RATED;
} else {
methodOfSort = NetworkUtils.POPULARITY;
textViewPopularity.setTextColor(getResources().getColor(R.color.teal_200));
textViewTopRated.setTextColor(getResources().getColor(R.color.white_color));
}
downloadData(methodOfSort, 1);
}
private void downloadData(int methodOfSort, int page) {
URL url = NetworkUtils.buildURL(methodOfSort, page);
Bundle bundle = new Bundle();
bundle.putString("url", url.toString());
loaderManager.restartLoader(LOADER_ID, bundle, this);
}
@Override
public Loader<JSONObject> onCreateLoader(int id, Bundle args) {
NetworkUtils.JSONLoader jsonLoader = new NetworkUtils.JSONLoader(this, args);
return jsonLoader;
}
@Override
public void onLoadFinished(Loader<JSONObject> loader, JSONObject data) {
ArrayList<Movie> movies = JSONUtils.getMoviesFromJSON(data);
if (movies != null && !movies.isEmpty()) {
viewModel.deleteAllMovies();
for (Movie movie : movies) {
viewModel.insertMovie(movie);
}
}
loaderManager.destroyLoader(LOADER_ID);
}
@Override
public void onLoaderReset(Loader<JSONObject> loader) {
}
}
Answer the question
In order to leave comments, you need to log in
DmitrySlv , in the second case, he clearly writes to you what is wrong (the classes do not match). And in the first, most likely, you are using the wrong manager (android.app.LoaderManager instead of androidx.loader.app.LoaderManager)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question