I am developing an application on Android.
I have an issue with a Fragment, the code can be found below.
The idea is to have an Image View display a list of Picture in an infinite loop. In order to realize this, I have created a new Thread, so as not to block the UI Thread. With a while (0 < 5) statement I create an infinite loop. Then I run an if...else statement to check on which Picture we are to determine the next picture to go to.
A Handler is used to take care of the 10 seconds delay between switching pictures. And finally another runnable takes care of the posting to the UI Thread.
This seems like a very complicated way of getting things done, anyone used the simpler code?
On top of that, somewhere in my code, there is an error. I cannot spot it, anyone?
Here is my code.
public class SecAct_Foto_Fragment extends Fragment {
    int counter = 0;
    View rootView;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.sec_act_photo_layout, container, false);
        return rootView;
    }
    Thread myThread = new Thread(new Runnable() {
        @Override
        public void run() {
            while (0 < 5) {
                //so far it loops only once
                //you start with run_rocks and but_left
                final ImageView pic_view = (ImageView) rootView.findViewById(R.id.foto_groot);
                final ImageView three_but = (ImageView) rootView.findViewById(R.id.knoppen);
                //create a runnable for the picture view
                pic_view.post(new Runnable() {
                    @Override
                    public void run() {
                        //every 10 seconds, switch picture and button fragment
                        if (counter == 0) {
                            final Handler handler0 = new Handler();
                            handler0.postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    pic_view.post(new Runnable() {
                                        @Override
                                        public void run() {
                                            pic_view.setImageResource(R.drawable.run_mount);
                                        }
                                    });
                                    counter = 1;
                                }
                            }, 10000L);
                        } else if (counter == 1) {
                            final Handler handler1 = new Handler();
                            handler1.postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    pic_view.post(new Runnable() {
                                        @Override
                                        public void run() {
                                            pic_view.setImageResource(R.drawable.run_away);
                                        }
                                    });
                                    counter = 2;
                                }
                            }, 10000L);
                        } else {
                            final Handler handler2 = new Handler();
                            handler2.postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    pic_view.post(new Runnable() {
                                        @Override
                                        public void run() {
                                            pic_view.setImageResource(R.drawable.run_rocks);
                                        }
                                    });
                                    counter = 0;
                                }
                            }, 10000L);
                        }
                    }
                });
                myThread.start();
            }
        }
    });
}

 
     
     
    