Android screen freezes, while saving to DB -


following coding downloading data web , on post execute save db , update listadapter gui.

problem when saving db, screen freezes time getting saved in db , 600 records 20 secs.

please let me know, how can change this, ui not freeze.

private class downloadwebpagetask extends asynctask<string, void, string> {      public downloadwebpagetask() {      }      @override     protected void onpreexecute() {          super.onpreexecute();                progressdialog = progressdialog.show(getactivity(),              "please wait...", "retrieving data ...", true);              progressdialog.setcancelable(true);               }          }        }      @override     protected string doinbackground(string... urls) {          string response = "";          (string url : urls) {              defaulthttpclient client = new defaulthttpclient();              httpget httpget = new httpget(url);              try {                  inputstream content = client.execute(httpget).getentity()                  .getcontent();                  bufferedreader buffer = new bufferedreader(                  new inputstreamreader(content));                  string s = "";                  while ((s = buffer.readline()) != null) {                      response += s;                  }              } catch (exception e) {                  e.printstacktrace();              }          }          return response;      }      @suppresslint("newapi")     @override     protected void onpostexecute(string result) {           try {                if (type.equalsignorecase("all courses")) {                 dblist = db.getallcoursedbs(type);                  if (dblist.isempty())                  {                 progressdialog.dismiss();                   }                   if((result==null)|| result.isempty())             {              }             else             {                 if (type.equalsignorecase("all courses")) {                     db.deleteallcoursebytypedb(type);                 }                 else                 {                     db.deleteallcoursebycategoryiddb(category_id);                  }                jsonarray jarray = new jsonarray(result);             (int = 0; < jarray.length(); i++)             {                 jsonobject json_data_one = jarray.getjsonobject(i);                   db.deleteallcoursecategorybytypedb(type);                   (int j = 0; < jarray.length(); i++)                  {                      coursedb nbnt = new coursedb();                       long insidestart = system.currenttimemillis();                      jsonobject json_data = jarray.getjsonobject(i);                     string crscd=null, crstitle=null;                     if (type.equalsignorecase("area of training")) {                                 crscd = json_data.getstring("coursecd");                           crstitle = json_data.getstring("coursetitle");                        }                     else{                       crscd = (json_data.getstring("crscd"));                        crstitle = (json_data.getstring("crstitle"));                     }                        nbnt.setcourse_crs(crscd);                      nbnt.setcategory_course_type(type);                      nbnt.setcourse_name(crstitle);                     nbnt.setcat_foreign_id(category_id);                       simpledateformat sdf = new simpledateformat("yyyymmdd_hhmmss");                     string currentdateandtime = sdf.format(new date());                     system.out.println("date inseted in db"+currentdateandtime);                     nbnt.setcourse_time(currentdateandtime);                     arrayofwebdata.add(nbnt);                     db.begintransaction();                      sqlitedatabase sqldb = db.getwritabledatabase();                       long starttime = system.currenttimemillis();                     db.addcoursebytype(nbnt, sqldb);                     db.settransactionsuccessful();                      db.endtransaction();                      db.close();                      long endtime = system.currenttimemillis();                        readwebpagerating();                      collections.sort(arrayofwebdata, new coursedbcomparator ());                     listadapter = new selectarraladapter(getactivity(),                                 arrayofwebdata);                          lv123.setadapter(listadapter);                         lv123.setfastscrollenabled(true);                   lv123.setonitemclicklistener(new onitemclicklistener() {                      public void onitemclick(adapterview<?> parent, view view,                      int position, long id) {                          coursedb planet = listadapter.getitem(position);                           string key = planet.getcourse_crs();                          string key_id_notebook = db.coursedb(key);                          system.out.println("value if key_id" + key_id_notebook);                           intent intent25 = new intent(getactivity(),                           coursedetailsactivity.class);                         intent25.putextra("course_id", key);                          intent25.putextra("category_id", category_id);                         intent25.putextra("type", type);                          intent25.putextra("category_name", category_name);                          startactivity(intent25);                         getactivity().finish();                      }                  });                }             }         }         }         catch (jsonexception e) {              log.e("log tag", "error parsing data" + e.tostring());          }      }  } 

changed code suggested, screen not freeze now, f move screen , crashes on post execute.

private class downloadwebpagetask extends asynctask<string, void, string> {      public downloadwebpagetask() {      }      @override     protected void onpreexecute() {          super.onpreexecute();          if (type.equalsignorecase("all courses")) {         dblist = db.getallcoursedbs(type);         if (dblist.isempty())          {              progressdialog = progressdialog.show(getactivity(),              "please wait...", "retrieving data ...", true);              progressdialog.setcancelable(true);          }         }else if(type.equalsignorecase("search")){             // nothing         }         else         {             dblist = db.getallcoursedbbytypes(category_id, type);             if (dblist.isempty())              {                  system.out.println("the value of dblist inside coursestypes"+dblist.size());                 progressdialog = progressdialog.show(getactivity(),                  "please wait...", "retrieving data ...", true);                  progressdialog.setcancelable(true);              }          }        }      @override     protected string doinbackground(string... urls) {          string response = "";          (string url : urls) {              defaulthttpclient client = new defaulthttpclient();              httpget httpget = new httpget(url);              try {                  inputstream content = client.execute(httpget).getentity()                  .getcontent();                  bufferedreader buffer = new bufferedreader(                  new inputstreamreader(content));                  string s = "";                  while ((s = buffer.readline()) != null) {                      response += s;                  }              } catch (exception e) {                  e.printstacktrace();              }               system.out.println("value of response"+response);                      //adding new */               if((response==null)|| response.isempty())             {              }             else             {                 if (type.equalsignorecase("all courses")) {                     db.deleteallcoursebytypedb(type);                 }else if(type.equalsignorecase("search")){                     // nothing                 }                 else                 {                     db.deleteallcoursebycategoryiddb(category_id);                  }                jsonarray jarray;             try {                 jarray = new jsonarray(response);              (int = 0; < jarray.length(); i++)             {                 jsonobject json_data_one = jarray.getjsonobject(i);                   system.out.println("all not empty");                  db.deleteallcoursecategorybytypedb(type);                      (int j = 0; < jarray.length(); i++)                  {                      coursedb nbnt = new coursedb();                       long insidestart = system.currenttimemillis();                      jsonobject json_data = jarray.getjsonobject(i);                     string crscd=null, crstitle=null;                     if (type.equalsignorecase("area of training")) {                         system.out.println("im area of tarinin");                                crscd = json_data.getstring("coursecd");                           crstitle = json_data.getstring("coursetitle");                        }                     else{                       crscd = (json_data.getstring("crscd"));                        crstitle = (json_data.getstring("crstitle"));                     }                      system.out.println("time 1 json parsing "                      + (system.currenttimemillis() - insidestart));                      nbnt.setcourse_crs(crscd);                      nbnt.setcategory_course_type(type);                      nbnt.setcourse_name(crstitle);                     nbnt.setcat_foreign_id(category_id);                       simpledateformat sdf = new simpledateformat("yyyymmdd_hhmmss");                     string currentdateandtime = sdf.format(new date());                     system.out.println("date inseted in db"+currentdateandtime);                     nbnt.setcourse_time(currentdateandtime);                     arrayofwebdata.add(nbnt);                     db.begintransaction();                      sqlitedatabase sqldb = db.getwritabledatabase();                       long starttime = system.currenttimemillis();                     db.addcoursebytype(nbnt, sqldb);                     db.settransactionsuccessful();                      db.endtransaction();                      db.close();                }             }             } catch (jsonexception e) {                 // todo auto-generated catch block                 e.printstacktrace();             }         }           }          return response;      }      @suppresslint("newapi")     @override     protected void onpostexecute(string result) {           if (type.equalsignorecase("all courses")) {             dblist = db.getallcoursedbs(type);              if (dblist.isempty())              {             progressdialog.dismiss();               }             }else if(type.equalsignorecase("search")){                 // nothing             }             else             {                 dblist = db.getallcoursedbbytypes(category_id, type);                  if (dblist.isempty())                  {                      system.out.println("the value of dblist inside coursestypes"+dblist.size());                     progressdialog.dismiss();                  }              }                    readwebpagerating();                  collections.sort(arrayofwebdata, new coursedbcomparator ());                 listadapter = new selectarraladapter(getactivity(),                             arrayofwebdata);                      lv123.setadapter(listadapter);                     lv123.setfastscrollenabled(true);               lv123.setonitemclicklistener(new onitemclicklistener() {                  public void onitemclick(adapterview<?> parent, view view,                  int position, long id) {                      coursedb planet = listadapter.getitem(position);                       string key = planet.getcourse_crs();                      string key_id_notebook = db.coursedb(key);                      system.out.println("value if key_id" + key_id_notebook);                      system.out.println("category id on lcick listnere inside post ecexute" + category_id);                      intent intent25 = new intent(getactivity(),                       coursedetailsactivity.class);                     intent25.putextra("course_id", key);                      intent25.putextra("category_id", category_id);                     intent25.putextra("type", type);                      intent25.putextra("category_name", category_name);                      startactivity(intent25);                     getactivity().finish();                  }              });      }  } 

the database operation should done in doinbackground()


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -