我需要从assets文件夹中读取文本文件(稍后将通过从Web服务获取json数据来替换),该文件包含json数据,然后将其保存到DB,然后从DB返回fetch并显示作为列表视图。 我有以下代码很慢,大约需要10秒才能显示一个包含270条记录的表。 我怎样才能加快速度。
这是从文件中获取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();
}
现在,一旦我将所有内容放入数据库中,我就会检索它:(
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