我正处于开发应用程序的后期阶段,该应用程序需要在第一次运行时从文本文件导入大量数据库。
我的问题是,在进行大量导入时,使用进度对话框显示的最佳方法是什么。负责的代码部分如下所示(该类的构造函数)。
public ManipulateDatabase(Activity contextIn) throws IOException {
baseHelper = new DatabaseHelper(contextIn);
context = contextIn;
BufferedReader pieceScan = new BufferedReader(new InputStreamReader(
getPieceTextFile(context), "UTF-8"));
BufferedReader specificScan = new BufferedReader(new InputStreamReader(
getSpecificTextFile(context), "UTF-8"));
BufferedReader elementScan = new BufferedReader(new InputStreamReader(
getElementTextFile(context), "UTF-8"));
BufferedReader quoteScan = new BufferedReader(new InputStreamReader(
getQuoteTextFile(context), "UTF-8"));
openSesame();
String line;
String pieceName;
String isDrama;
String point;
String element;
String quote;
String isCharacter;
int commaPoint;
int commaPoint2;
if (initialLaunch) {
System.out.println("INITIAL LAUNCH BLOCK INITIATED");
while ((line = pieceScan.readLine()) != null) {
System.out.println("PieceInsertion: ");
commaPoint = line.indexOf(",");
pieceName = line.substring(0, commaPoint);
isDrama = line.substring(commaPoint + 1, commaPoint + 2);
ContentValues pv = new ContentValues();
pv.put(pieceField, pieceName);
pv.put(typeField, (Integer.parseInt(isDrama)));
dataBase.insert(pieceTable, null, pv);
System.out.println(line
+ ": INSERTION SHOULD HAVE JUST FINISHED");
}
while ((line = specificScan.readLine()) != null) {
System.out.println("SpecificInsertion: ");
commaPoint = line.indexOf(",");
point = line.substring(0, commaPoint);
commaPoint2 = (line
.substring(commaPoint + 1, line.length() - 1)
.indexOf(","))
+ point.length();
// pieceName = line.substring(commaPoint + 1, commaPoint2 + 1);
element = line.substring(commaPoint2 + 2, line.length());
ContentValues sv = new ContentValues();
sv.put(pointField, point);
// sv.put(pieceField, pieceName);
sv.put(elementField, element);
dataBase.insert(specificTable, null, sv);
System.out.println(line
+ ": INSERTION SHOULD HAVE JUST FINISHED");
}
while ((line = elementScan.readLine()) != null) {
commaPoint = line.indexOf(",");
element = line.substring(0, commaPoint);
commaPoint2 = (line
.substring(commaPoint + 1, line.length() - 1)
.indexOf(","))
+ element.length();
pieceName = line.substring(commaPoint + 1, commaPoint2 + 1);
isCharacter = line.substring(commaPoint2 + 2, commaPoint2 + 3);
ContentValues ev = new ContentValues();
ev.put(elementField, element);
ev.put(pieceField, pieceName);
ev.put(elTypeField, (Integer.parseInt(isCharacter)));
dataBase.insert(elementTable, null, ev);
System.out.println(line
+ ": INSERTION SHOULD HAVE JUST FINISHED");
}
while ((line = quoteScan.readLine()) != null) {
commaPoint = line.indexOf("^");
quote = line.substring(0, commaPoint);
System.out.println("Quote = " + quote);
point = line.substring(commaPoint + 1, line.length());
System.out.println("Point = " + point);
ContentValues qv = new ContentValues();
qv.put(quoteField, quote);
qv.put(pointField, point);
dataBase.insert(quoteTable, null, qv);
System.out.println(line
+ ": INSERTION SHOULD HAVE JUST FINISHED");
}
}
}
答案 0 :(得分:0)
您应该使用异步任务来执行数据库加载。
http://developer.android.com/reference/android/os/AsyncTask.html
在onPreExecute()中,您可以显示进度对话框,在onPostExecute()中,您应该删除ProgressDialog。数据库加载应该在doInBackground()中完成。
有很多例子。点击此处了解更多信息
How to use AsyncTask correctly in Android
你的整个ManipulateDatabase()应该进入doInBackground()。