Actualisation ProgressBar dans un thread
Bonjour,
Je débute en programmation Android et je suis bloqué sur le développement de mon application.
Avant toute chose, je veux ouvrir un document Excel (.xls) et l'afficher directement sur mon application, pour cela je passe par un TableLayout et je parcours mon Excel pour remplir mon TableLayout de TextRow et TextView.
Cette partie fonctionne, néanmoins le chargement de gros fichier prenant du temps je veux afficher une barre de progression en fonction d'où on en est dans le parcours du fichier Excel.
Pour cela j'ai créer une progressBar initialisée à 0 et le maximum au nombre de ligne de mon fichier Excel.
Je passe ensuite tout cela dans un thread et c'est là que ça se complique, les textview nécessitant de rappeler le thread UI ..
J'ai en résultat l'affichage de mon tableau et de ma progressBar, mais seulement lorsque l'application a finit de parcourir mon tableau (quand c'est terminé quoi..). Donc j'ai toujours mon temps de chargement, et ma progressbar qui est soit à 0 soit complète..
Voici mon code ci dessous en espérant que vous pourrez m'aider
Bonne journée,
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
| private void readExcelFile(final Context context, final String filepath) {
new Thread(new Runnable() {
public void run() {
if (!isExternalStorageAvailable() || isExternalStorageReadOnly())
{
Log.e(TAG, "Storage not available or read only");
return;
}
final int num_param = getNumberofRows(context,filepath);
final int num_col = getNumberofColumns(context, filepath);
ProgressHandler.post(new Runnable() {
public void run() {
progress_bar.setMax(num_param);
progress_bar.setProgress(0);
}
});
runOnUiThread(new Runnable() {
@Override
public void run() {
try {
// Creating Input Stream
File file = new File(filepath);
FileInputStream myInput = new FileInputStream(file);
// Create a POIFSFileSystem object
POIFSFileSystem myFileSystem = new POIFSFileSystem(myInput);
// Create a workbook using the File System
HSSFWorkbook myWorkBook = new HSSFWorkbook(myFileSystem);
// Get the first sheet from workbook
HSSFSheet mySheet = myWorkBook.getSheetAt(0);
/** We now need something to iterate through the cells.**/
Iterator rowIter = mySheet.rowIterator();
//réinitialise les vues et le tableau
mTableLayout.removeAllViews();
mTableColumn.removeAllViews();
mTableLayoutHeader.removeAllViews();
compt_header=0;
compt_col=0;
while(rowIter.hasNext()){
//create a tablerow
TableRow tr = new TableRow(getApplicationContext());
//creation de la ligne
HSSFRow myRow = (HSSFRow) rowIter.next();
Iterator cellIter = myRow.cellIterator();
//creation des cellules de la ligne
while(cellIter.hasNext()){
HSSFCell myCell = (HSSFCell) cellIter.next();
//Create a textview.//
TextView trCell = new TextView(getApplicationContext());
//Set TextView text, color,size of text, size of cell//
trCell.setText(String.valueOf(myCell));
trCell.setTextSize(17);
trCell.setGravity(Gravity.CENTER);
trCell.setWidth(ColumnWidths[compt_col]);
trCell.setHeight(20);
if( compt_header==0 )//si 1ère ligne (=titre colonne)
{
trCell.setTextColor(WHITE);
trCell.setBackgroundResource(R.drawable.cell_header);
tr.addView(trCell);
}
else if (compt_header!=0) //si pas 1ère ligne
{
if(compt_col == 0) //si 1ère colonne
{
trCell.setTextColor(WHITE);
trCell.setBackgroundResource(R.drawable.cell_header);
mTableColumn.addView(trCell);
}
else if (compt_col !=0 )// si pas 1ère colonne
{
trCell.setTextColor(BLACK);
trCell.setBackgroundResource(R.drawable.cell_shape);
tr.addView(trCell);
}
}
compt_col++;
//fin cellule
}
compt_col=0; //réinitialise à 0, on va à la ligne suivante => colonne=0
if(compt_header==0) {
compt_header++;
mTableLayoutHeader.addView(tr); //Add tablerow to the tablelayout
}
else if(compt_header!=0) {
compt_header++;
mTableLayout.addView(tr); //Add tablerow to the tablelayout
}
//fin ligne
}
/**** Update the progress bar ****/
ProgressHandler.post(new Runnable() {
public void run() {
progress_bar.setProgress(compt_header);
}
});
//fin tableau
}catch (Exception e){e.printStackTrace(); }
}
});
}
}).start();
} |