我的代码有问题,我对安卓世界很陌生,我需要AsyncTask类的帮助.
我有一种方法应该搜索CSV文件中的行,然后考虑到它找到了多少行--它应该向TableLayout添加特定的行,这要归功于一个地图,它指示要添加哪些视图以及按什么顺序添加它们。
这个方法可以像我想的那样工作,但是问题是由于计算,它冻结了我的应用程序,我想做一些事情来指示用户正在发生什么事情。
我读过一些关于使用AsyncTask的文章,但我知道AsyncTask在后台线程中进行计算,并且不能通过后台线程更新UI
希望你能理解我(我是法语,所以我的英语是近似^^)
这是我的密码:
private void addModifLinesToView(TableLayout view, String ressource, Map<String,Object> ViewVal, List<List<View>> listeViews) throws IOException {
try {
CSVFile csvBT = new CSVFile(new FileInputStream(Environment.getExternalStorageDirectory()+"/"+folder_main+"/"+imported_data+"/"+ressource));
List<String[]> tableauVals = csvBT.findLines(indicePoste.getText().toString());
int numLigne = 0;
//pour chaque ligne trouvée dans le csv
for (String[] s : tableauVals) {
numLigne++;
if (view.getId() == tableauBT.getId())
{
numLigneBT++;
}
List<View> actualViews = new ArrayList<>();
List<View> dividers = new ArrayList<>();
TableRow row = new TableRow(getApplicationContext());
row.setLayoutParams(new TableLayout.LayoutParams(
TableLayout.LayoutParams.MATCH_PARENT,
TableLayout.LayoutParams.WRAP_CONTENT));
//creation des separateurs
for (int i = 0; i < ViewVal.size()+1; i++) {
View divider = new View(getApplicationContext());
divider.setLayoutParams(new TableLayout.LayoutParams(
TableLayout.LayoutParams.MATCH_PARENT,
TableLayout.LayoutParams.MATCH_PARENT));
divider.setBackgroundColor(Color.BLACK);
dividers.add(divider);
}
//delimiter a gauche de la ligne
row.addView(dividers.get(0),
new TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.MATCH_PARENT));
ArrayList<String> listeModeles;
for ( Map.Entry<String,Object> entry : ViewVal.entrySet()) {
if (entry.getKey().contains("EditText")) {
EditText et = new EditText(getApplicationContext());
actualViews.add(et);
et.setMaxWidth(0);
et.getBackground().mutate().setColorFilter(getResources().getColor(android.R.color.darker_gray), PorterDuff.Mode.SRC_ATOP);
et.setHighlightColor(getResources().getColor(R.color.gris));
et.setPadding(3, 3, 3, 7);
et.setGravity(Gravity.CENTER);
et.setTextColor(Color.BLACK);
et.setSingleLine();
et.setInputType((int) entry.getValue());
et.setImeOptions(EditorInfo.IME_ACTION_DONE);
//pour mettre la taiile du text en SP
et.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
} else if (entry.getKey().contains("Spinner")) {
Spinner spin = new Spinner(getApplicationContext());
actualViews.add(spin);
spin.getBackground().setColorFilter(getResources().getColor(R.color.noir), PorterDuff.Mode.SRC_ATOP);
spin.setPadding(3, 3, 3, 3);
spin.setGravity(Gravity.CENTER);
spin.setPopupBackgroundResource(R.color.blanc);
ByteArrayInputStream bais = new ByteArrayInputStream((byte[])entry.getValue());
listeModeles = listerModeles(bais);
ArrayAdapter<String> adapter = new ArrayAdapter<>(MainActivity.this, android.R.layout.simple_spinner_item, listeModeles);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin.setAdapter(adapter);
} else if (entry.getKey().contains("TextView"))
{
TextView tv = new TextView(getApplicationContext());
actualViews.add(tv);
tv.setText(Integer.toString(numLigne));
tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
tv.setMaxWidth(0);
tv.setPadding(3, 3, 3, 7);
tv.setGravity(Gravity.CENTER);
tv.setTextColor(Color.BLACK);
tv.setSingleLine();
}
}
for (int j = 0; j < actualViews.size(); j++) {
row.addView(actualViews.get(j),
new TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.MATCH_PARENT, 1f));
row.addView(dividers.get(j + 1),
new TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.MATCH_PARENT));
}
view.addView(row);
//separateur horizontal
View dividerH = new View(getApplicationContext());
dividerH.setLayoutParams(new TableLayout.LayoutParams(
TableLayout.LayoutParams.MATCH_PARENT,
3));
dividerH.setBackgroundColor(Color.BLACK);
view.addView(dividerH, new TableLayout.LayoutParams(
TableLayout.LayoutParams.MATCH_PARENT,
3));
listeViews.add(actualViews);
}
}
catch (ArrayIndexOutOfBoundsException | FileNotFoundException e) {
e.printStackTrace();
}
}发布于 2018-07-26 07:50:12
请检查一下文档,还有一个例子:
private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
protected Long doInBackground(URL... urls) {
int count = urls.length;
long totalSize = 0;
for (int i = 0; i < count; i++) {
totalSize += Downloader.downloadFile(urls[i]);
publishProgress((int) ((i / (float) count) * 100));
// Escape early if cancel() is called
if (isCancelled()) break;
}
return totalSize;
}
protected void onProgressUpdate(Integer... progress) {
setProgressPercent(progress[0]);
}
protected void onPostExecute(Long result) {
showDialog("Downloaded " + result + " bytes");
}
}您的方法addModifLinesToView应该从doInBackground调用。您只需添加publishProgress调用,就可以在该方法上向用户显示您正在处理的文件。
https://stackoverflow.com/questions/51533375
复制相似问题