【发布时间】:2016-07-03 17:11:54
【问题描述】:
在我的初始屏幕中,我在从远程服务器下载数据时显示了一个进度条。 我正在从远程数据库下载 3 个数据表。 每张桌子可以有不同的大小。 每个表下载过程由不同的函数调用。 我想在下载数据的同时计算和更新进度条。
例如: 表 1 - 100 行 表 2 - 30 行 表 3 - 230 行
下载从方法doInBckground调用的数据:
@Override
protected void doInBackground(String... params) {
downloadTable1();
downloadTable2();
downloadTable3();
}
我想到了两种方法来实现这一点: 1) 从所有 3 个表中获取数据大小,然后在开始下载之前计算进度条上每行下载数据的百分比。 对于上面的示例,要下载 100+30+230=360 行。 将要下载的每一行都会以 0.27% 的速度前进进度条
2) 或者设置从高级到每张表的比例,然后相应设置进度量。例如,如果我假设表 3 始终是最大的,则表 2 是最小的,表 1 在中间。 所以我可以通过以下方式修复它们对进度条的影响: 表 1 30% 表 2 20% 表 3 50% 然后在每个下载表格的方法中只为该数量取得进展。
我的问题是,实现这一目标的常用方法是制作准确的进度条,在从 3 个大小可能不同的数据库表下载数据时,该进度条将正确进行。
下载数据的方法之一的示例:
//Download Table1 data and save it in SQLite MAINCAT Table
private void downloadTable1() {
//-1 Code in PHP file to get Table1
String respond = getHttpRespond(-1); //The String respond got from HTTP request
DB_SQLiteManager db = new DB_SQLiteManager(context); //Prepare SQLite database object for future downloaded data
try {
JSONObject jsonObject = new JSONObject(respond); //the result in JSON
JSONArray jsonArray = jsonObject.getJSONArray("posts"); //result in Array of JSON
for (int i = 0; i < jsonArray.length(); i++) {
//Update progressbar value
totalProgressBarValue = totalProgressBarValue+i;
publishProgress(totalProgressBarValue);
JSONObject curr = jsonArray.getJSONObject(i); //Get current line
String strID = curr.optJSONObject("post").optString("id"); //Get current string ID
String strName = curr.optJSONObject("post").optString("name"); //Get current string et_Name
String imageLink = curr.optJSONObject("post").optString("imagelink");
String strPosition = curr.optJSONObject("post").optString("position");
//Insert row of data to SQLite MAINCAT table
db.addDataLineToTableMAINCAT(Integer.parseInt(strID), strName, imageLink, Integer.parseInt(strPosition));
System.out.println("ADDEDD: " + strID + strName + imageLink + strPosition);
}
} catch (Exception e) {
e.printStackTrace();
}
}
谢谢
【问题讨论】:
-
请显示downloadTable方法
标签: android database progress-bar splash-screen