我有问题,但我没有找到任何解决方案,所以我正在写。我正在尝试创建一个从波兰股票下载和解析数据的应用程序。我想每隔30秒更新一次公司的价值,我这样做但是我无法刷新当前标签,如果我想看到新值我如何切换标签两次然后再回来,然后我可以看到新值。我想在不切换标签的情况下更新数据。
MainActivity
pobieranie_wartosci.start();
//this starts Thread for updating data
// init tabhost
this.initializeTabHost(savedInstanceState);
// init ViewPager
this.initializeViewPager();
}
private void initializeViewPager() {
List<Fragment> fragments = new Vector<Fragment>();
fragments.add(new Tab1Fragment());
fragments.add(new Tab2Fragment());
fragments.add(new Tab1Fragment());
fragments.add(new Tab1Fragment());
fragments.add(new Tab3Fragment());
fragments.add(new Tab1Fragment());
this.myViewPagerAdapter = new MyFragmentPagerAdapter(
getSupportFragmentManager(), fragments);
this.viewPager = (ViewPager) super.findViewById(R.id.viewPager);
this.viewPager.setAdapter(this.myViewPagerAdapter);
this.viewPager.setOnPageChangeListener(this);
onRestart();
}
我也在这里放置我更新数据的线程
Thread pobieranie_wartosci = new Thread()
{
public void run()
{
while(true)
{
int r = zmienne.lista_moich_spolek.size()-1;
for(int i =0; i<=r;i++){
try
{
zmienne.lista_moich_spolek.get(i).transformacja_ceny(new Cena().execute(zmienne.lista_moich_spolek.get(i).numer).get());
} catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
zmienne.lista_moich_spolek.get(0).cena_kupna++;
System.out.println("cena :" + zmienne.lista_moich_spolek.get(0).cena_kupna);
}
try {
Thread.sleep(1500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
};
最后可能有用的是MyPageAdapter
public class MyFragmentPagerAdapter extends FragmentPagerAdapter {
List<Fragment> fragments;
public MyFragmentPagerAdapter(FragmentManager fm, List<Fragment> fragments) {
super(fm);
this.fragments = fragments;
}
@Override
public Fragment getItem(int position) {
return this.fragments.get(position);
}
@Override
public int getCount() {
return fragments.size();
}
}