Vijay Laxmi
Vijay Laxmi

Reputation: 175

AsyncTask in fragments not updating UI

I am using FragmentPagerAdapter to swipe Fragment and using AsyncTask to update gridview but its not updating the UI.

Its not updating the UI and also not throwing any error I tried to check the flow and its running GalleryTab fragment twice...and unable to understand the problem .

My code:-

public class GalleryTab extends Fragment {
            GridView gridview;
            ProgressDialog mProgressDialog;
            GridViewAdapter adapter;
            public List<GalleryList> phonearraylist = null;
            View view;
            private WeakReference<RemoteDataTask> asyncTaskWeakRef;

        public static Fragment newInstance(Context context) {
            GalleryTab f = new GalleryTab();
        return f;
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            // Inflate the layout for this fragment
            view = inflater.inflate(R.layout.activity_gallery_tab, null);
             setRetainInstance(true);
                startNewAsyncTask();
            //new RemoteDataTask(this).execute();
            return inflater.inflate(R.layout.activity_gallery_tab, container, false);
        }

        // RemoteDataTask AsyncTask
            private class RemoteDataTask extends AsyncTask<Void, Void, Void> {
                private WeakReference<GalleryTab> fragmentWeakRef;

                private RemoteDataTask (GalleryTab gallerytab) {
                    this.fragmentWeakRef = new WeakReference<GalleryTab>(gallerytab);
                }

                @Override
                protected void onPreExecute() {
                    super.onPreExecute();

                } 

                @Override
                protected Void doInBackground(Void... params) {
                    // Create the array

                    phonearraylist = new ArrayList<GalleryList>();
                    try {
                        for (int i = 0; i <= 6; i++) {
                            GalleryList map = new GalleryList();
                            map.setGallery("http://oi39.tinypic.com/21oydxs.jpg");
                            System.out.println("PRINT!!!!--  "+ i);
                            phonearraylist.add(map);
                        }
                    } catch (ParseException e) {
                        Log.e("Error", e.getMessage());
                        e.printStackTrace();
                    }

                    return null;
                }

                @Override
                protected void onPostExecute(Void result) {
                    // if (this.fragmentWeakRef.get() != null) {
                    adapter = new GridViewAdapter(getActivity(), phonearraylist);
                    System.out.println("PRINT SIZE --  "+ phonearraylist.size());
                    gridview = (GridView)view.findViewById(R.id.gridview);
                    gridview.setAdapter(adapter);

                //  mProgressDialog.dismiss();
                    // }
                }
            }
            private void startNewAsyncTask() {
                RemoteDataTask asyncTask = new RemoteDataTask(this);
                this.asyncTaskWeakRef = new WeakReference<RemoteDataTask >(asyncTask );
                asyncTask.execute();
            }
    }

Upvotes: 2

Views: 2299

Answers (2)

Piyush
Piyush

Reputation: 18923

You have to find out your id of GridView onCreateView() method because it returns the view...

gridview = (GridView)view.findViewById(R.id.gridview);

And yes if you have Make a object of View at intialize then just replace :

return inflater.inflate(R.layout.activity_gallery_tab, container, false);

to

return view;

Upvotes: 2

JRomero
JRomero

Reputation: 4868

Not sure this is the particular issue but it appears that there is a lot of weaving between the RemoteDataTask scope and the GalleryTab. Try setting your RemoteDataTask to static and everything you lose reference to you should be passing in to the RemoteDataTask constructor. Maybe this would help you get your debugging in line.

This:

private class RemoteDataTask extends AsyncTask<Void, Void, Void>

To:

private static class RemoteDataTask extends AsyncTask<Void, Void, Void>

This is the prefered way to deal with multiple threads (which is what AsyncTasks are).

Upvotes: 0

Related Questions