AsyncTask的片段中没有更新的用户界面 [英] AsyncTask in fragments not updating UI

查看:122
本文介绍了AsyncTask的片段中没有更新的用户界面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 FragmentPagerAdapter 刷卡片段和使用的AsyncTask 更新GridView的,但它没有更新的用户界面。

它不更新用户界面,也没有抛我试图检查流量及其运行GalleryTab片段两次的错误......而无法理解的问题。

我的code: -

 公共类GalleryTab扩展片段{
            GridView控件GridView控件;
            ProgressDialog mProgressDialog;
            GridViewAdapter适配器;
            公开名单< GalleryList> phonearraylist = NULL;
            查看图。
            私人的WeakReference< RemoteDataTask> asyncTaskWeakRef;

        公共静态片段的newInstance(上下文的背景下){
            GalleryTab F =新GalleryTab();
        返回F;
        }

        @覆盖
        公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,
                捆绑savedInstanceState){
            //充气的布局该片段
            鉴于= inflater.inflate(R.layout.activity_gallery_tab,NULL);
             setRetainInstance(真正的);
                startNewAsyncTask();
            //新RemoteDataTask(本).execute();
            返回inflater.inflate(R.layout.activity_gallery_tab,集装箱,假);
        }

        // RemoteDataTask的AsyncTask
            私有类RemoteDataTask扩展的AsyncTask<虚空,虚空,虚空> {
                私人的WeakReference< GalleryTab> fragmentWeakRef;

                私人RemoteDataTask(GalleryTab gallerytab){
                    this.fragmentWeakRef =新的WeakReference< GalleryTab>(gallerytab);
                }

                @覆盖
                在preExecute保护无效(){
                    super.on preExecute();

                }

                @覆盖
                保护无效doInBackground(虚空...... PARAMS){
                    //创建数组

                    phonearraylist =新的ArrayList< GalleryList>();
                    尝试 {
                        的for(int i = 0; I< = 6;我++){
                            GalleryList地图=新GalleryList();
                            map.setGallery(http://oi39.tinypic.com/21oydxs.jpg);
                            的System.out.println(打印!!!!  - + I);
                            phonearraylist.add(图)
                        }
                    }赶上(ParseException的E){
                        Log.e(错误,e.getMessage());
                        e.printStackTrace();
                    }

                    返回null;
                }

                @覆盖
                保护无效onPostExecute(无效的结果){
                    //如果(this.fragmentWeakRef.get()!= NULL){
                    适配器=新GridViewAdapter(getActivity(),phonearraylist);
                    的System.out.println(打印尺寸 - + phonearraylist.size());
                    gridview的=(GridView控件)view.findViewById(R.id.gridview);
                    gridview.setAdapter(适配器);

                // mProgressDialog.dismiss();
                    //}
                }
            }
            私人无效startNewAsyncTask(){
                RemoteDataTask的AsyncTask =新RemoteDataTask(本);
                this.asyncTaskWeakRef =新的WeakReference< RemoteDataTask>(AsyncTask的);
                asyncTask.execute();
            }
    }
 

解决方案

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

 的GridView =(GridView控件)view.findViewById(R.id.gridview);
 

是的,如果你有做在intialize查看的对象,然后只需更换:

 返回inflater.inflate(R.layout.activity_gallery_tab,集装箱,假);
 

 返回查看;
 

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

解决方案

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;

这篇关于AsyncTask的片段中没有更新的用户界面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆