简体   繁体   中英

how to make infinite loop in android?

I d like to make a while(true) in my code but ,when i run, i don t see anything: I've tried with AsyncTask but i think that a infinite loop it's too heavy.

public class openActivity extends AppCompatActivity {
 ArrayList<String> myArraylist = new Arraylist<> ();
 CustomAdapter myAdapter;
 ListView list;
 protected void onCreate(Bundle savedInstanceState) {
     setContentView(R.layout.myActivity);
     list = findViewById(R.id.myList);
     try {
         while (true) {
             Document doc = Jsoup.connect(myUrl).get;
             //here i get info i need
             //add that info to myArraylist
             myAdapter = new CustomAdapter(this, myArraylist);
             list.setAdapter(myAdapter);
             myAdapter.notifyDataSetChanged();
             while (myArrayList.get(0).equals(myArraylist.get(0))) {
                 Thread.sleep(900000);
             }
         }
     } catch (Exception ex) {
         ex.getLocalizeMessage()
     }
 }

Your loop is blocking the UI thread. To perform a periodic task you should use a ScheduledExecutorService .

Also, avoid recreating the adapter on every iteration. Update the existing data and use DiffUtils to notify the changes.

or you can try this :

myAdapter = new CustomAdapter(this, myArraylist);
list.setAdapter(myAdapter);
getData();

public void getData(){
         Document doc = Jsoup.connect(myUrl).get;
         //here i get info i need
         //add that info to myArraylist
         myAdapter.notifyDataSetChanged();
         getData()
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM