Android Listview item Change after 10 seconds automatically?

◇◆丶佛笑我妖孽 提交于 2019-12-10 19:08:56

问题


i have two arraylist one is departurelist and one is arrival list i want to show this list

alternatively means after 10 seconds list is change automatically

first 10 seconds departure list and next 10 second arrival list i will put my code here i will try usint runnable but my apps is hang when run this code

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

回答1:


Handler handler = new Handler();

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

//do anything


}
}, 10000);

this code is almost same. you can try to create a 'recursive call' of both the methods with a delay of 10 seconds




回答2:


I wouldn't always create new ListAdapters. I would create one for departures and one for arrivals and hold them in memory. Then I would simply swap the adapter in the ListView in the UIThread. Since swapping the adapter has consequences for the UI this should be done in UIThread. I would then update the data of the adapter which is currently not in use in the background. In the end you only need only one background thread to keep your data up to date.

But generally you should read into Loaders and Oberserables and Observers and try using this approach. Not only does Android then take away all the complex threading and thread syncing for you, but you have a nice extendable and platform conform approach.



来源:https://stackoverflow.com/questions/13854955/android-listview-item-change-after-10-seconds-automatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!