user2236580
user2236580

Reputation: 99

Code after thread not being executed

I am working on extracting some html for an android app. A problem I have is that the code after the thread in the code below is not being executed. I feel like I'm making an amateur mistake but I don't see what it is. I know the arraylists are being populated, from printing the their contents into logcat in the createTimetable() method. However anything after }).start(); is not being done. Any ideas?

    protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_display_online_timetable);
    Intent intent = getIntent();
    value = intent.getStringExtra("url");


      new Thread(new Runnable() {
            public void run() {
                try {
                    Log.d("url",value);
                    doc = Jsoup.connect(value).get();
                    Element monday = doc.select("p ~ table").first();
                    Element tuesday = doc.select("p ~ table").get(1);
                    Element wednesday = doc.select("p ~ table").get(2);
                    Element thursday = doc.select("p ~ table").get(3);
                    Element friday = doc.select("p ~ table").get(4);

                    monday.select("tr:eq(0)").remove();
                    tuesday.select("tr:eq(0)").remove();
                    wednesday.select("tr:eq(0)").remove();
                    thursday.select("tr:eq(0)").remove();
                    friday.select("tr:eq(0)").remove();


                    final Elements mon = monday.select("td");
                    final Elements tues = tuesday.select("td");
                    final Elements wed = wednesday.select("td");
                    final Elements thurs = thursday.select("td");
                    final Elements fri = friday.select("td");               

                    Log.d("thread","sdsddssd");
                    createTimetable(monClasses, mon, day1);
                    createTimetable(tuesClasses, tues, day2);
                    createTimetable(wedClasses, wed, day3);
                    createTimetable(thurClasses, thurs, day4);
                    createTimetable(friClasses, fri, day5);


                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
          }).start();


      Log.d("sdfsdfsdf", "HELLO");
      for(int i = 0; i<monClasses.size();i++)
      {
          Log.d("monday", monClasses.get(i).toString());
      }


    mSectionsPagerAdapter = new SectionsPagerAdapter(
            getSupportFragmentManager());

}

logcat

Upvotes: 2

Views: 145

Answers (3)

Onur A.
Onur A.

Reputation: 3017

try to assign thread to a variable and start that thread variable at end of onCreate method

Upvotes: 0

GaBo
GaBo

Reputation: 246

Since you aren't doing any UI work after the thread can you not do the work in a finally clause?

If you are relying on this thread to gather data to fill views you should use AsyncTask.

Upvotes: 1

meredrica
meredrica

Reputation: 2563

I guess you come from traditional java development so here's a word of warning: Threads and Android do not like each other. Switch your thread to an Async Task and try again, it's only a few lines of code to change.

Basically, copy all your code to the doInBackground() method and then call task.execute(). Everything you want to do with the data that was generated should go into the onPostExecute() method of the Async task. You can even do parallel processing by using publishResult() and onResultPublished() Note that this might run synchronous but you should not have to worry about that.

Upvotes: 2

Related Questions