Trishant Sharma
Trishant Sharma

Reputation: 125

onLoadFinished called with wrong data even if loadInBackground returns correct data

I am making an app to display the movies list as shown in the screenshot.

The problem is that when I select the options menu top_rated option it reloads the correct data but the list returned to onLoadFinished contains the previous data with the new data after that.

How do I rectify this as I want only the new data to be in the list. I tried onLoaderReset but it is not called because the activity is not destroyed and I am not able to find any other way to dereference the old data or in other words first empty the old already fetched list and then replace it with a newly fetched list.

How to achieve the above..?

And there is one more thing, that whenever I go to any other activity and then return back the data shown is duplicated. I know that this might be a lifecycle issue and can be solved with some research but I can do that only if the first issue is resolved.

Here is the screenshot of the app:- Screenshot of MainActivity UPDATE --> The actual problem was not in MainActivity.java but in the below file and hence the problem is solved now as mentioned in my answer.

Here is my JSONUtils.java function which I was calling in MainActivity.java:-

import com.example.trishantsharma.popularmovies.Movie;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

public class JSONUtils {
private static ArrayList<String[]> movieTitleAndPosterList = new ArrayList<>();
//ALL CONSTANTS HERE
public static ArrayList<String[]> parseMovieJSON(String jsonReceived){
    //THE PROBLEM IS HERE AND HAS BEEN RECTIFIED IN THE ANSWER
    try {
        JSONObject movieObject = new JSONObject(jsonReceived);
        JSONArray allMoviesArray = movieObject.getJSONArray(RESULT_JSON_ARRAY_STRING);
        for (int i = 0; i < 19; i++) {
            JSONObject particularMovie = allMoviesArray.optJSONObject(i);
            String movieId = Integer.toString(particularMovie.optInt(ID_JSON_OBJECT_STRING));
            String moviePoster = particularMovie.optString(POSTER_JSON_OBJECT_STRING);
            String[] singleMovieTitleAndPosterArray = {movieId,moviePoster};
            movieTitleAndPosterList.add(singleMovieTitleAndPosterArray);
        }
        return movieTitleAndPosterList;
    } catch (JSONException e){
        e.printStackTrace();
    }
    return null;
}
}

Thanks.

Upvotes: 1

Views: 124

Answers (2)

Trishant Sharma
Trishant Sharma

Reputation: 125

Here is the answer I referenced to in my question:-

Here is my JSONUtils.java function which I was calling in MainActivity.java:-

public static ArrayList<String[]> parseMovieJSON(String jsonReceived){
    movieTitleAndPosterList.clear();
    try {
        JSONObject movieObject = new JSONObject(jsonReceived);
        JSONArray allMoviesArray = movieObject.getJSONArray(RESULT_JSON_ARRAY_STRING);
        for (int i = 0; i < 19; i++) {
            JSONObject particularMovie = allMoviesArray.optJSONObject(i);
            String movieId = Integer.toString(particularMovie.optInt(ID_JSON_OBJECT_STRING));
            String moviePoster = particularMovie.optString(POSTER_JSON_OBJECT_STRING);
            String[] singleMovieTitleAndPosterArray = {movieId,moviePoster};
            movieTitleAndPosterList.add(singleMovieTitleAndPosterArray);
        }
        return movieTitleAndPosterList;
    } catch (JSONException e){
        e.printStackTrace();
    }
    return null;
}

Since the ArrayList here was static therefore I had to clear it before adding new data.

Since, I was not clearing it the data was always getting appended to the list and hence the list returned to onLoadFinished had duplicated items.

Thanks @KavinRajuS for taking the time in helping me solve this query. Hence, the problem is now fixed and the app is working well.

Upvotes: 1

Kavin Raju S
Kavin Raju S

Reputation: 1382

This can be done by clearing the data in the RecyclerView Adapter, when you click an option in the menu, and then reload the data.

This goes inside your RecyclerView Adapter Class.

 public void cleatMovieData(){
    int size = movieDetails.size();
    if (size > 0) {
        for (int i = 0; i < size; i++) {
            movieDetails.remove(0);
        }
        notifyItemRangeRemoved(0,size);
    }
}

You can call this method before you call the Loader to Restart.

Upvotes: 2

Related Questions