【发布时间】:2015-11-06 06:36:14
【问题描述】:
我在我的 Android 应用程序中使用ProgressBar 并在提取数据库期间显示值,但ProgressBar 值超过 100 并显示到 122%
我试过设置,
if(progress<=100)
publishProgress(progress);
但这会导致长时间显示 100%。
谁能帮助进度计算响应文件大小和提取。
这是我的代码:
@Override
protected Integer doInBackground(Void... params) {
byte[] buf = new byte[8192];
String name = null;
try {
System.out.println("doInBackground");
listener.onDoInBackground(context,true);
name = "db_sqlite.7z";
//InputStream of the sqlite file
InputStream in = context.getResources().openRawResource(R.raw.db_sqlite);
int fileSize = in.available();
FileOutputStream out = context.openFileOutput("db.sqlite", 0);//Context.MODE_PRIVATE);
try {
/* In contrast to other classes in org.tukaani.xz,
LZMAInputStream doesn't do buffering internally
and reads one byte at a time. BufferedInputStream
gives a huge performance improvement here but even
then it's slower than the other input streams from
org.tukaani.xz.
in = new BufferedInputStream(in); */
in = new XZInputStream(in);
int size;
int counter=0;
while ((size = in.read(buf)) != -1) {
//System.out.write(buf, 0, size);
if(this.isCancelled())
break;
out.write(buf, 0, size);
counter++;
progress = (int) (counter*100*1024/(double)fileSize);
publishProgress(progress);
}
protected void onProgressUpdate(Integer... values) {
//System.out.println("onProgressUpdate");
super.onProgressUpdate(values);
listener.onProgressUpdation(context, true, values);
}
在活动中:
@Override
public void onProgressUpdation(Context context, Boolean isStarted, Integer... values) {
tv_you_can_change.setText(context.getResources().getString(R.string.extracting_database) + " " + values[0] + "%");
tv_you_can_change.setVisibility(View.VISIBLE);
progressBar.setProgress(values[0]);
//System.out.println("onProgressUpdation");
}
【问题讨论】:
-
在将值设置为进度条之前检查
values[0]的值。即,在.setProgress(values[0])之前 -
@govindpatel:问题出在进度 = (int) (counter*100*1024/(double)fileSize);
标签: android android-activity android-asynctask android-sqlite android-progressbar