0

I have a FragmentActivity that's parsing JSON and updates the view. OnCreate I have a ProgressBar initialized when my view is still in the load process. Then after the loading is done, I set

progressBar.setVisibility(View.GONE);

but it would not take affect. I did make sure that its exactly same instance by doing toString() on the progressBar variable and its exactly same. I also made sure that its actually reaches the point. Also I donn't have any onClicks here, it simply needs to dissapear when content is loaded.

I watched this tutorial and it works for the guy there.

Method that's setting the visibility to GONE

  public void updateUI(){
   if(movies != null) {
       for (int i = 0; i < array1.length; i++) {
           //Log.v("PATH in array1 " , movies.get(i).getPath());
           array1[i] = (movies.get(i).getPath());
       }

       gridView.setAdapter(imageAdapter);


   }else{
       Log.v("MOVIE ARRAY", "- I HAVE NO DATA");
   }

    Log.v("HIDE", "BEFORE");
    Log.v("PROGRESS_BAR", progressBar.toString());
    progressBar.setVisibility(View.GONE);

    Log.v("HIDE", "AFTER");

}

My onCreate method that creates the variable

    @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View v = inflater.inflate(R.layout.fragment_main, null);
    this.progressBar = (ProgressBar)v.findViewById(R.id.progress_bar);

    Log.v("PROGRESS_BAR", progressBar.toString());

    if (savedInstanceState != null){
        Log.v("onCreate ", "EXISTING STATE");
        this.movies = savedInstanceState.getParcelableArrayList("movies");
    }else{
        Log.v("onCreate ", "EMPTY STATE");
    } 

    setHasOptionsMenu(true);


}

onStart

 @Override
public void onStart() {
    super.onStart();
    Log.v("onStart ", "START");
    //Log.v("Movies Size ", String.valueOf(movies.size()));
            update();
}

update()

 private void update(){

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
    String sortType = prefs.getString(getString(R.string.pref_sort_key), getString(R.string.pref_sort_default));

    if (sortType.equals(sortTypeSaved)){
        Log.v("Equals", "YAY");
        Log.v(sortType.toString(), sortTypeSaved.toString());

        if (movies == null || movies.isEmpty()){

            Log.v("movies ARRAY", "EMPTY");
            DownloadJsonDataTask asyncDownload = new DownloadJsonDataTask();
            asyncDownload.execute(sortType);
        }else{
            Log.v("Equals", "NOPE");
            Log.v("movies ARRAY", " not EMPTY");
            updateUI();
        }
    }else{
        Log.v(sortType.toString(), sortTypeSaved.toString());
       // if (movies == null || movies.isEmpty()){
        DownloadJsonDataTask asyncDownload = new DownloadJsonDataTask();
            asyncDownload.execute(sortType);
      //  }else{
       // }
    }

in case if asynctask was run then i call it from onPostExacute

  @Override
    protected void onPostExecute(List<Movie> movies) {
        updateUI();
    }

Here is the screen of the log where you can see that its definately the same instance enter image description here

Please let me know what am I doing wrong, I have been on this all day and cannot find the solution. Thank you

Jenny
  • 445
  • 2
  • 6
  • 23
  • Are you calling the UI update from the AsyncTask? That's probably the issue. Let me know if referencing the activity and calling an interface works and I'll create an answer. The link below has an example. http://stackoverflow.com/questions/12252654/android-how-to-update-an-ui-from-asynctask-if-asynctask-is-in-a-separate-class – kevintcoughlin Sep 19 '15 at 22:17

1 Answers1

4

I wonder how I missed it the first time.You are inaflating the view two time. Once at

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View v = inflater.inflate(R.layout.fragment_main, null);

and then again at

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_main, container, false);

Which is why it is not working. Do not inflate in onStart() and it will work!

roide
  • 127
  • 1
  • 4