android - Trouble with making a method into an AsyncTask -


i'm have frustratingly tough time figuring out. guess i'm not understanding how correctly assemble asynctask. have method in class want run asynctask instead of bogging down when gets called. can help? in advance.

private void getdatesnames() { jsonparser jparser = new jsonparser();  jsonobject json = jparser.getjsonfromurl(url);  arraylist<string> dates = new arraylist<string>(); arraylist<string> teams = new arraylist<string>();   try {      contacts = json.getjsonarray(tag_contacts);       for(int = 0; < contacts.length(); i++){         jsonobject c = contacts.getjsonobject(i);          if ((c.getstring(tag_email1)).contains(league)) {              // storing each json item in variable             string id = c.getstring(tag_id);             string email1 = c.getstring(tag_email1);             string email2 = c.getstring(tag_email2);               dates.add(id);             teams.add(email1);             teams.add(email2);         }     }  } catch (jsonexception e) {     e.printstacktrace(); }   linkedhashset hs1 = new linkedhashset(); linkedhashset hs2 = new linkedhashset();  hs1.addall(dates); hs2.addall(teams); dates.clear(); teams.clear(); dates.addall(hs1); teams.addall(hs2);      (int = 0; < dates.size(); ++i) {     adapter1.add(dates.get(i)); }  (int = 0; < teams.size(); ++i) {      adapter2.add(teams.get(i));  } 

just stick slow stuff in doinbackground. filling in 2 arrays, better make them private final, different aspects can see them.

 asynctask<void, void, void> newtask =  new asynctask<void, void, void>() {          private final arraylist<string> dates = new arraylist<string>();         private final arraylist<string> teams = new arraylist<string>();         @override         protected void onpreexecute() {             //you dont' need here, leave out onpreexecute()...         }          @override         protected arraylist<string> doinbackground(void... params) {             jsonparser jparser = new jsonparser();              jsonobject json = jparser.getjsonfromurl(url);              try {                  contacts = json.getjsonarray(tag_contacts);                  for(int = 0; < contacts.length(); i++){                     jsonobject c = contacts.getjsonobject(i);                      if ((c.getstring(tag_email1)).contains(league)) {                          // storing each json item in variable                         string id = c.getstring(tag_id);                         string email1 = c.getstring(tag_email1);                         string email2 = c.getstring(tag_email2);                           dates.add(id);                         teams.add(email1);                         teams.add(email2);                     }                 }              } catch (jsonexception e) {                 e.printstacktrace();             }               linkedhashset hs1 = new linkedhashset();             linkedhashset hs2 = new linkedhashset();              hs1.addall(dates);             hs2.addall(teams);             dates.clear();             teams.clear();             dates.addall(hs1);             teams.addall(hs2);          }          @override         protected void onpostexecute(arraylist<string> filelist) {             // dismiss dialog if necessary             (int = 0; < dates.size(); ++i) {                 adapter1.add(dates.get(i));             }              (int = 0; < teams.size(); ++i)             {                  adapter2.add(teams.get(i));              }         }      };     newtask.execute(); 

Comments

Popular posts from this blog

Unable to remove the www from url on https using .htaccess -