Android Listview项目会在10秒后自动更改吗?

时间:2012-12-13 07:23:21

标签: android listview runnable

我有两个arraylist,一个是出发名单,一个是到达列表,我想显示此列表

或者表示10秒后列表自动更改

前10秒出发名单和下一个10秒到达名单 我将把我的代码放在这里我将尝试usint runnable但我的应用程序在运行此代码时挂起

  

Home activity.java

  final int []sliderImageArray={R.drawable.banner,R.drawable.banner01,R.drawable.banner02};
        final int []footerImageArray={R.drawable.bottomadv,R.drawable.sandwich,R.drawable.underbig};
        try 
        {

            Log.e("Land Scape Run","");
            Log.e("LandScape  Run","Handler ");
                final Handler imagehandler = new Handler();

                Runnable runnable;

                runnable = new Runnable()
                {

                    int i=0;
                    public void run()
                    {   // slider image run
                        imageslider.setImageResource(sliderImageArray[i]);  //Log.e("Image Run",""+sliderImageArray[i]);
                        i++;
                        if(i>sliderImageArray.length-1)
                        {
                                i=0;    
                                clear();
                        }
                        imagehandler.postDelayed(this, 4000); // for interval
                    }

                };
                imagehandler.postDelayed(runnable,10);

                final Handler footerimagehandler =new Handler();
                runnable = new Runnable()
                {   
                    int j=0;
                    public void run()
                    {   // footer image run
                        imagefooter.setImageResource(footerImageArray[j]); //   Log.e("Image Run",""+footerImageArray[j]);
                        j++;
                        if(j>footerImageArray.length-1)
                        {
                                j=0;    
                                clear();
                        }
                        footerimagehandler.postDelayed(this, 5000); // for interval
                    }
                };

                footerimagehandler.postDelayed(runnable, 10);


                // For ListView Change after 10 seconds;
                final Handler listhandler= new Handler();
                runnable = new Runnable() {

                    public void run() {
                        // departure flight list

                                if(!flightList.isEmpty())
                                {
                                        int displaymode=getResources().getConfiguration().orientation;
                                        if(displaymode==1)
                                        {
                                                textviewinfo.setText("Departure Flight List");
                                                ListAdapter adapter = new SimpleAdapter(HomeActivity.this,flightList,
                                                        R.layout.listportrait,
                                                        new String[] { TAG_MDESTINATION, TAG_MFLIGHT, TAG_MAIRLINE,TAG_MSCHEDULE,TAG_MTERMINALGATE,TAG_MFSTATUS  }, new int[] {
                                                                R.id.textdestination, R.id.textflight, R.id.textairline,R.id.textschedule,R.id.texttermgate,R.id.textstatus });
                                                setListAdapter(adapter);

                                        }
                                        else
                                        {
                                               textviewcity.setText(textcity);
                                               textviewairport.setText(textairport);
                                               textviewinfo.setText("Departure Flight List");

                                            ListAdapter adapter = new SimpleAdapter(HomeActivity.this,flightList,
                                                    R.layout.list,
                                                    new String[] { TAG_MDESTINATION, TAG_MFLIGHT, TAG_MAIRLINE,TAG_MSCHEDULE,TAG_MTERMINALGATE,TAG_MFSTATUS  }, new int[] {
                                                            R.id.textdestination, R.id.textflight, R.id.textairline,R.id.textschedule,R.id.texttermgate,R.id.textstatus });
                                            setListAdapter(adapter);

                                        }
                                }listhandler.postDelayed(this,1000);// if loop complete departure
                            // arrival flight list
                                if(!arrivalList.isEmpty())
                                {

                                        int displaymode=getResources().getConfiguration().orientation;
                                        if(displaymode==1)
                                        {
                                            text1.setText("Origin");
                                            text4.setText("Arrival");
                                            textviewinfo.setText("Arrival Flight List");

                                                ListAdapter adapter = new SimpleAdapter(HomeActivity.this,arrivalList,
                                                        R.layout.listportrait,
                                                        new String[] { TAG_MDESTINATION, TAG_MFLIGHT, TAG_MAIRLINE,TAG_MSCHEDULE,TAG_MTERMINALGATE,TAG_MFSTATUS  }, new int[] {
                                                                R.id.textdestination, R.id.textflight, R.id.textairline,R.id.textschedule,R.id.texttermgate,R.id.textstatus });
                                                setListAdapter(adapter);

                                        }
                                        else
                                        {
                                                text1.setText("Origin");
                                                text4.setText("Arrival");
                                                textviewinfo.setText("Arrival Flight List");

                                            ListAdapter adapter = new SimpleAdapter(HomeActivity.this,arrivalList,
                                                    R.layout.list,
                                                    new String[] { TAG_MDESTINATION, TAG_MFLIGHT, TAG_MAIRLINE,TAG_MSCHEDULE,TAG_MTERMINALGATE,TAG_MFSTATUS  }, new int[] {
                                                            R.id.textdestination, R.id.textflight, R.id.textairline,R.id.textschedule,R.id.texttermgate,R.id.textstatus });
                                            setListAdapter(adapter);

                                        }
                                }listhandler.postDelayed(this,100);// if complete arrrival

                    }
                };
                listhandler.postDelayed(runnable, 10);

2 个答案:

答案 0 :(得分:3)

Handler handler = new Handler();

handler.postDelayed(new Runnable() {
@Override
public void run() {

//do anything


}
}, 10000);

这段代码几乎相同。您可以尝试创建两个方法的“recursive call”,延迟时间为10秒

答案 1 :(得分:2)

我不会总是创建新的ListAdapter。我会创建一个用于离开,一个用于到达并将它们保存在记忆中。然后我只需在UIThread中的ListView中交换适配器。由于交换适配器会对UI产生影响,因此应在UIThread中完成。然后,我将更新当前未在后台使用的适配器的数据。最后,您只需要一个后台线程来保持数据的最新状态。

但通常你应该阅读Loaders and Oberserables和Observers并尝试使用这种方法。 Android不仅为您带走了所有复杂的线程和线程同步,而且还有一个很好的可扩展和平台一致的方法。