更新ListFragment AsyncTask的后 [英] Update ListFragment after asynctask

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

问题描述

我有两个listfragment和我的应用程序一个片段。我会告诉你第一个片段。

应用程序启动,AsyncTask的检索数据,并把它们放在arrayNews。
我认为这个问题是在listfragment列表视图或适配器不刷新的更新。

如果我更改手机的方向=> listfragment(+列表视图)正确显示。
如果我去(在2和3内存片段)到片段3,然后回来对片段1 =>它工作正常。

对不起,我的英语^^

公共类MainActivity扩展FragmentActivity实现ActionBar.TabListener
{
    私人SectionsPagerAdapter mSectionsPagerAdapter;
    私人ViewPager mViewPager;
    公共静态的ArrayList<数据> arrayNews =新的ArrayList<数据>();    @覆盖
    公共无效的onCreate(捆绑savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);        清单<片断> listeFragments =新的矢量<片断>();
        listeFragments.add(Fragment.instantiate(在此,ActualiteFragment.class.getName()));
        listeFragments.add(Fragment.instantiate(在此,DossierFragment.class.getName()));
        listeFragments.add(Fragment.instantiate(在此,ForumFragment.class.getName()));
        mSectionsPagerAdapter =新SectionsPagerAdapter(getSupportFragmentManager(),listeFragments);        最后的动作条动作条= getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);        mViewPager =(ViewPager)findViewById(R.id.pager);
        mViewPager.setAdapter(mSectionsPagerAdapter);        mViewPager.setOnPageChangeListener(新ViewPager.SimpleOnPageChangeListener()
        {
            @覆盖
            公共无效使用onPageSelected(INT位置)
            {
                actionBar.setSelectedNavigationItem(位置);
            }
        });        的for(int i = 0; I< mSectionsPagerAdapter.getCount();我++)
        {
            actionBar.addTab(actionBar.newTab().setText(mSectionsPagerAdapter.getPageTitle(i)).setTabListener(this));
        }        如果(arrayDossier.size()== 0安培;&安培; arrayDossier.size()== 0)
        {
            chargement_donnees();
        }
    }    公共无效chargement_donnees()
    {
        AsyncTaskChargement chargNews =新AsyncTaskChargement();
        chargNews.execute();
    }    公共类AsyncTaskChargement扩展的AsyncTask<无效,整型,太虚>
    {
        私人ProgressDialog对话框=新ProgressDialog(MainActivity.this);        @覆盖
        在preExecute保护无效()
        {
            super.on preExecute();
            dialog.setMessage(Chargement DES最近搜索...);
            dialog.setIndeterminate(真);
            dialog.setCancelable(假);
            dialog.show();
        }        @覆盖
        保护无效doInBackground(虚空......为arg0)
        {
            arrayNews = ContainerData_news.getFeeds();
            返回null;
        }        @覆盖
        保护无效onPostExecute(虚空结果)
        {
            如果(this.dialog.isShowing())
            {
                this.dialog.dismiss();
            }
        }
    }    @覆盖
    公共布尔onCreateOptionsMenu(菜单菜单)
    {
        。getMenuInflater()膨胀(R.menu.menu_main,菜单);
        返回true;
    }    @覆盖
    公共布尔onOptionsItemSelected(菜单项项)
    {
        开关(item.getItemId())
        {
        案例R.id.menu_refresh:
            chargement_donnees();
            返回true;
        默认:
            返回false;
        }
    }    公共无效onTabUnselected(ActionBar.Tab选项卡,FragmentTransaction fragmentTransaction)
    {
    }    公共无效onTabSelected(ActionBar.Tab选项卡,FragmentTransaction fragmentTransaction)
    {
        mViewPager.setCurrentItem(tab.getPosition());
    }    公共无效onTabReselected(ActionBar.Tab选项卡,FragmentTransaction fragmentTransaction)
    {
    }    公共类SectionsPagerAdapter扩展FragmentPagerAdapter
    {
        清单<片断> listeFragments;        公共SectionsPagerAdapter(FragmentManager FM,列表与LT;片断> listeFragments)
        {
            超(FM);
            this.listeFragments = listeFragments;
        }        @覆盖
        公共片段的getItem(INT位置)
        {
            返回listeFragments.get(位置);
        }        @覆盖
        公众诠释getCount将()
        {
            返回listeFragments.size();
        }        @覆盖
        公共CharSequence的getPageTitle(INT位置)
        {
            开关(位置)
            {
            情况下0:
                返回的getString(R.string.title_section1).toUpperCase();
            情况1:
                返回的getString(R.string.title_section2).toUpperCase();
            案例2:
                返回的getString(R.string.title_section3).toUpperCase();
            }
            返回null;
        }
    }    公共静态类ActualiteFragment扩展ListFragment
    {        公共ActualiteFragment()
        {
        }        @覆盖
        公共无效onActivityCreated(捆绑savedInstanceState)
        {
            super.onActivityCreated(savedInstanceState);
            setListAdapter(新AdapterListe(getActivity(),arrayNews));
        }        @覆盖
        公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,捆绑savedInstanceState)
        {
            返回inflater.inflate(R.layout.listfragment,集装箱,FALSE);
        }        @覆盖
        公共无效onListItemClick(ListView中升,视图V,INT位置,长的ID)
        {
            ...
        }
    }
}


解决方案

我发现了一个简单的方法来更新该名单
步骤是:
1. ListFragment声明onCreateView

 公开查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
            捆绑savedInstanceState){
        // TODO自动生成方法存根
        super.onCreateView(充气器,容器,savedInstanceState);
        查看查看= inflater.inflate(R.layout.TheLayoutContainingAListView,集装箱,FALSE);
        返回视图。
    }

TheLayoutContainingAListView.xml应该只包含一个ListView

 <?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:方向=垂直>
        < ListView控件
            机器人:ID =@机器人:ID /列表
            机器人:layout_width =FILL_PARENT
            机器人:layout_height =FILL_PARENT>
        < /&的ListView GT;
< / LinearLayout中>

Android的:ID必须是@android:ID /列表。但是你可以尝试其他的东西..我没有..
2.在您的AsyncTask的onPostExecute(AsyncTask的应该是MainActivity),做你想要做的一切..即宣布一个适配器,setAdapter

 保护无效onPostExecute(字符串结果){
            // TODO自动生成方法存根
            LV =(ListView控件)findViewById(android.R.id.list);
            CustomAdapter适应=新CustomAdapter(MainActivity.this,
                    android.R.id.list,取); //你可以使用ArrayAdapter ..我想一个定制
            lv.setAdapter(适应);
            showFragment(您的片段,FALSE);            super.onPostExecute(结果);
        }

showFragment只是我用来显示一个特定的片段,并隐藏所有其他..你可以很容易地通过FragmentTransaction展()和隐藏实现它()方法的功能。

I have two listfragment and one fragment in my application. I show you first fragment.

The application starts, Asynctask retrieves data and put them in arrayNews. I think the problem is the update of listview or adapter in listfragment that does not refresh.

If I change the phone's orientation => listfragment (+ listview) appears correctly. If I go to fragment 3 (fragment in 2 and 3 in memory), then come back on fragment 1 => it works correctly.

Sorry for my english ^^

public class MainActivity extends FragmentActivity implements ActionBar.TabListener
{
    private SectionsPagerAdapter mSectionsPagerAdapter;
    private ViewPager mViewPager;
    public static ArrayList<Data> arrayNews = new ArrayList<Data>();

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        List<Fragment> listeFragments = new Vector<Fragment>();
        listeFragments.add(Fragment.instantiate(this, ActualiteFragment.class.getName()));
        listeFragments.add(Fragment.instantiate(this, DossierFragment.class.getName()));
        listeFragments.add(Fragment.instantiate(this, ForumFragment.class.getName()));
        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager(), listeFragments);

        final ActionBar actionBar = getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mSectionsPagerAdapter);

        mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener()
        {
            @Override
            public void onPageSelected(int position)
            {
                actionBar.setSelectedNavigationItem(position);
            }
        });

        for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++)
        {
            actionBar.addTab(actionBar.newTab().setText(mSectionsPagerAdapter.getPageTitle(i)).setTabListener(this));
        }

        if (arrayDossier.size() == 0 && arrayDossier.size() == 0)
        {
            chargement_donnees();
        }       
    }

    public void chargement_donnees()
    {
        AsyncTaskChargement chargNews = new AsyncTaskChargement();
        chargNews.execute();
    }

    public class AsyncTaskChargement extends AsyncTask<Void, Integer, Void>
    {
        private ProgressDialog dialog = new ProgressDialog(MainActivity.this);

        @Override
        protected void onPreExecute()
        {
            super.onPreExecute();
            dialog.setMessage("Chargement des données ...");
            dialog.setIndeterminate(true);
            dialog.setCancelable(false);
            dialog.show();
        }

        @Override
        protected Void doInBackground(Void... arg0)
        {
            arrayNews = ContainerData_news.getFeeds(); 
            return null;
        }

        @Override
        protected void onPostExecute(Void result)
        {
            if (this.dialog.isShowing())
            {
                this.dialog.dismiss();
            }
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        switch (item.getItemId())
        {
        case R.id.menu_refresh:
            chargement_donnees();
            return true;
        default:
            return false;
        }
    }

    public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction)
    {
    }

    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction)
    {
        mViewPager.setCurrentItem(tab.getPosition());
    }

    public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction)
    {
    }

    public class SectionsPagerAdapter extends FragmentPagerAdapter
    {
        List<Fragment> listeFragments;

        public SectionsPagerAdapter(FragmentManager fm, List<Fragment> listeFragments)
        {
            super(fm);
            this.listeFragments = listeFragments;
        }

        @Override
        public Fragment getItem(int position)
        {
            return listeFragments.get(position);
        }

        @Override
        public int getCount()
        {
            return listeFragments.size();
        }       

        @Override
        public CharSequence getPageTitle(int position)
        {
            switch (position)
            {
            case 0:
                return getString(R.string.title_section1).toUpperCase();
            case 1:
                return getString(R.string.title_section2).toUpperCase();
            case 2:
                return getString(R.string.title_section3).toUpperCase();
            }
            return null;
        }
    }

    public static class ActualiteFragment extends ListFragment
    {       

        public ActualiteFragment()
        {
        }

        @Override
        public void onActivityCreated(Bundle savedInstanceState)
        {
            super.onActivityCreated(savedInstanceState);
            setListAdapter(new AdapterListe(getActivity(), arrayNews));
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {           
            return inflater.inflate(R.layout.listfragment, container, false);
        }

        @Override
        public void onListItemClick(ListView l, View v, int position, long id)
        {
            ...
        }
    }
}

解决方案

I found an easy way to update such list Steps would be.: 1. declare onCreateView in ListFragment

public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreateView(inflater, container, savedInstanceState);
        View view = inflater.inflate(R.layout.TheLayoutContainingAListView, container, false);
        return view;        
    }

TheLayoutContainingAListView.xml should contain only a listview

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
        <ListView
            android:id="@android:id/list"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" >
        </ListView>
</LinearLayout>

The android:id must be "@android:id/list" . however you may try other things.. I didn't.. 2. In onPostExecute of your AsyncTask (Asynctask should be in MainActivity), do everything that you want to do.. i.e. declare an adapter, and setAdapter

protected void onPostExecute(String result) {
            // TODO Auto-generated method stub
            lv = (ListView) findViewById(android.R.id.list);
            CustomAdapter adapt = new CustomAdapter(MainActivity.this,
                    android.R.id.list, fetch); //You can use ArrayAdapter.. i wanted a custom one
            lv.setAdapter(adapt);
            showFragment(YOUR Fragment, false);

            super.onPostExecute(result);
        }

showFragment is just a function i used to show a particular fragment and hide all others.. you can easily achieve it by FragmentTransaction show() and hide() methods.

这篇关于更新ListFragment AsyncTask的后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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