2

我正在使用FragmentPagerAdapter滑动Fragment和使用AsyncTask来更新 gridview,但它不更新 UI。

它没有更新 UI,也没有抛出任何错误我试图检查流程及其运行的 GalleryTab 片段两次......并且无法理解问题。

我的代码:-

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

2 回答 2

2

您必须找出您的 GridView onCreateView() 方法的 ID,因为它返回视图...

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

是的,如果您在初始化时创建了一个 View 对象,那么只需替换:

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

return view;
于 2013-08-22T05:00:43.210 回答
0

不确定这是特定问题,但似乎在RemoteDataTask范围和GalleryTab. 尝试将您设置RemoteDataTask为静态,并且您失去对您的引用的所有内容都应该传递给RemoteDataTask构造函数。也许这会帮助您进行调试。

这个:

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

至:

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

这是处理多线程的首选方式(这就是 AsyncTasks)。

于 2013-08-22T05:02:09.920 回答