gpt4 book ai didi

android - 异步任务后更新 ListFragment

转载 作者:行者123 更新时间:2023-11-29 22:03:33 27 4
gpt4 key购买 nike

我的应用程序中有两个列表 fragment 和一个 fragment 。我给你看第一个 fragment 。

应用程序启动,Asynctask 检索数据并将它们放入 arrayNews 中。我认为问题是listview或adapter in listfragment的更新没有刷新。

如果我更改手机的方向 => listfragment (+ listview) 会正确显示。如果我转到 fragment 3(内存中的 fragment 2 和 3),然后返回 fragment 1 => 它工作正常。

抱歉我的英语不好^^

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)
{
...
}
}
}

最佳答案

我找到了更新此类列表的简单方法步骤是:1.在ListFragment中声明onCreateView

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 应该只包含一个 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>

android:id 必须是 "@android:id/list"。但是你可以尝试其他的东西..我没有..2. 在你的 AsyncTask 的 onPostExecute 中(Asynctask 应该在 MainActivity 中),做你想做的一切......即声明一个适配器,然后设置适配器

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 只是我用来显示特定 fragment 并隐藏所有其他 fragment 的函数。您可以通过 FragmentTransaction show() 和 hide() 方法轻松实现它。

关于android - 异步任务后更新 ListFragment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11359649/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com