0
我有一個要求從資產文件夾讀取文本文件(稍後它將被從Web服務獲取json數據取代),它包含json數據和然後將其保存到數據庫,然後從數據庫中取回並顯示爲列表視圖。 我有下面的代碼很慢,大約需要10秒來顯示一個有270條記錄的表。 我怎樣才能讓它快速。從Json讀取,保存到數據庫並重新取回需要很長的時間
這是功能從文件中獲取並存儲JSON數據在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();
}
,一旦我把一切都放在DB我找回它:(
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;
}
評論我後
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();
}
}
不過,對於400條記錄來說,這同樣需要16秒
Asmita
Put Yout beginTransaction()和endTransaction()之間的所有插入查詢引用此參考http://stackoverflow.com/questions/8147440/android-database-transaction/8163179#8163179 – bindal
我不明白,我不想要做這些事情,只是想讓它更快地查詢,這兩種方式在json閱讀,以及插入/從DB – Asmi
當您在for循環執行插入查詢時獲取,它需要很長時間插入,但是當您將插入代碼之間的事務查詢得到更快 – bindal