我在这里有一个代码
File file = new File(FilePathname_CUS);
CsvReader csvReader = new CsvReader();
try (CsvParser csvParser = csvReader.parse(file, StandardCharsets.UTF_8)) {
CsvRow row;
if (count_row(FilePathname_SAMPLE) != 0) {
myDb.delete_customer(current_email);
int index = 0;
while ((row = csvParser.nextRow()) != null) {
myDb.insert_customer(
row.getField(0),
row.getField(1),
row.getField(2),
row.getField(3),
row.getField(4),
row.getField(5),
row.getField(6),
row.getField(7),
row.getField(8),
row.getField(9)
);
index++;
float p = (float)index / (float)row_count;
p = p * (float)100;
//mProgressAnimation.setProgress((int)p);
counter_load.setText((int)p);
}
}
}
对于每个循环,我都将数据保存在数据库中,但我的目标是根据当前百分比更新textview,但是发生的情况是textview仅在while循环之后更改,我如何在循环期间进行更新?
答案 0 :(得分:0)
通过使用新的线程和处理程序。 尝试以下代码:
File file = new File(FilePathname_CUS);
CsvReader csvReader = new CsvReader();
try (CsvParser csvParser = csvReader.parse(file, StandardCharsets.UTF_8)) {
if (count_row(FilePathname_SAMPLE) != 0) {
myDb.delete_customer(current_email);
Thread thread;
final Handler handler = new Handler();
thread = new Thread(new Runnable() {
public void run() {
int index = 0;
CsvRow row;
while ((row = csvParser.nextRow()) != null) {
myDb.insert_customer(
row.getField(0),
row.getField(1),
row.getField(2),
row.getField(3),
row.getField(4),
row.getField(5),
row.getField(6),
row.getField(7),
row.getField(8),
row.getField(9)
);
index++;
float p = (float)index / (float)row_count;
p = p * (float)100;
handler.post(new Runnable() {
public void run() {
counter_load.setText((int)p);
}
});
try {
// Sleep for 100 milliseconds.
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
thread.start();
}
}