0

I have a requirement to read a text file from assets folder(later it will be replaced by getting json data from web services), which is containing json data and then save it to DB, then fetch is back from DB and display as listview. I have the following code which is slow, take about 10 secs to display a table having 270 records. How can I make it fast.

This is function to get the json data from the file and store in DB

try {


    InputStream is=getActivity().getAssets().open("Programme.txt");

            int size = is.available();
            byte[] buffer = new byte[size];
            is.read(buffer);
            is.close();
            String bufferString = new String(buffer);
            System.out.println("the output is"+bufferString);

            //convert string to JSONArray
            JSONArray jArray = new JSONArray(bufferString);
            if(bufferString.isEmpty())
            {

            }
            else
            {
                db.deleteAllCourseCategoryByTypeDB(type);
            }

            System.out.println("All the data"+bufferString);
            boolean eventFlag = false;

            for (int i = 0; i < jArray.length(); i++)

            {

                System.out.println("this is where I am "+i);
                JSONObject json_data = jArray.getJSONObject(i);
    String entityNum =      (json_data.getString("entityNum"));
    String entityDescr = (json_data.getString("entityDescr"));
    String courseCount = (json_data.getString("courseCount"));
    System.out.println("the entityNum is"+entityNum);

    CourseCategoryDB nbnt = new CourseCategoryDB();

        nbnt.setcourse_cat_id(entityNum);
        nbnt.setcourse_cat_type(type);
    nbnt.setcourse_number(courseCount);
    nbnt.setcoursecat_name(entityDescr);
    db.addcoursecategory(nbnt);


    }




        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();

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

Now , once I put everything in DB I retrieve it :(

public List<CourseDB> getAllCourseDBByTypes(String key_id,String type) {
    List<CourseDB> NBList = new ArrayList<CourseDB>();


    String selectQuery = "SELECT  * FROM " + TABLE_COURSE + " WHERE "
            + CATEGORY_ID_FOR + "=?";

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, new String[] { key_id });

    // looping through all rows and adding to list
    if (cursor.moveToFirst()) {
        do {
            CourseDB NB = new CourseDB();
            NB.setcourse_id(cursor.getInt(0));
            NB.setcat_foreign_id(cursor.getString(4));
            NB.setcourse_crs(cursor.getString(3));
            NB.setcategory_course_type(cursor.getString(2));
            // NB.setName(cursor.getString());
            NB.setcourse_name(cursor.getString(1));
    // Adding contact to list
            NBList.add(NB);
        } while (cursor.moveToNext());
    }
    cursor.close();
    db.close();
    System.out.println("the total values in list"+NBList.size());
    return NBList;
}

After the comment I changed code to this.

            for (int j = 0; j < jArray.length(); j++) {
                    //

                    JSONObject json_data = jArray.getJSONObject(j);
                    String crsCd = (json_data.getString("crsCd"));
                    System.out.println("value of CRSCD and j"+crsCd+""+j);
                    String crsTitle = (json_data
                            .getString("crsTitle"));
                    String courseType = type;
                    String foreign_category_id=id_notebook2;

                    CourseDB nbnt = new CourseDB();

                    nbnt.setcourse_crs(crsCd);
                    nbnt.setcategory_course_type(type);
                    nbnt.setcourse_name(crsTitle);
                    nbnt.setcat_foreign_id(foreign_category_id);


                    final long startTime = System.currentTimeMillis();



                    db.beginTransaction();
                    try
                    {
                    db.addcourseByType(nbnt);

                        db.setTransactionSuccessful();


                        //Error in between database transaction 


                    }
                      finally {
                          db.endTransaction();
                      }

                }

Still, it is same taking 16 sec for 400 records.

Asmita

Asmi
  • 301
  • 3
  • 13
  • Put Yout All insert queries between beginTransaction() and endTransaction() refer this for reference http://stackoverflow.com/questions/8147440/android-database-transaction/8163179#8163179 – bindal Jan 21 '13 at 09:59
  • I dont understand, I donot want to do that stuff, just want to make it query faster, both ways in json reading and as well as inserting/fetching from DB – Asmi Jan 21 '13 at 10:12
  • when you executing insert queries in for loop it takes long time for inserting but when you put your insertion code between transaction the queries get faster – bindal Jan 21 '13 at 10:14
  • I added what you suggesting still it is the same any ideas? @bindal – Asmi Jan 22 '13 at 02:56
  • first find out which portion of your code taking too long time,whether it is fetching from txt,inserting in db.displaying by calculating starttime and endtime after that we can find out – bindal Jan 22 '13 at 04:51

1 Answers1

0

I think that you can speed up this by using adapters (e.g. SimpleCursorAdapter) for insetting your Cursor in the List. Plus, I think update rows in DB is better than deleting and inserting.

Jeevuz
  • 13
  • 1
  • 8