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