我需要从 assets 文件夹中读取一个文本文件(稍后它将被从 web 服务中获取 json 数据所取代),其中包含 json 数据,然后将其保存到 DB,然后从 DB 中取回并显示为 listview。我的以下代码很慢,大约需要 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 秒。
阿斯米塔