下面是我的 Table 类,它从 XML 创建一个 tableview AsyncTask
:
public class Table extends Activity {
readXML readxml = new readXML();
private ProgressDialog dialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.table);
ThreadPolicy tp = ThreadPolicy.LAX;
StrictMode.setThreadPolicy(tp);
performBackgroundTask mTask = new performBackgroundTask(
getBaseContext());
mTask.execute();
// readxml.read();
}
private void tableView() {
// TODO Auto-generated method stub
TableLayout t1;
TableLayout tl = (TableLayout) findViewById(R.id.main_table);
TableRow tr_head = new TableRow(this);
tr_head.setId(10);
tr_head.setBackgroundColor(Color.parseColor("#afeeee"));
tr_head.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
TextView label_id = new TextView(this);
label_id.setId(20);
label_id.setText("id");
label_id.setTextSize(18);
label_id.setTextColor(Color.BLACK);
label_id.setPadding(5, 5, 5, 5);
tr_head.addView(label_id);// add the column to the table row here
TextView labelName = new TextView(this);
labelName.setId(21);// define id that must be unique
labelName.setText(" Name"); // set the
// text for
// the
// header
labelName.setTextSize(18);
labelName.setTextColor(Color.BLACK); // set the color
labelName.setPadding(5, 5, 5, 5); // set the padding (if required)
tr_head.addView(labelName); // add the column to the table row
TextView labelUnit = new TextView(this);
labelUnit.setId(21);// define id that must be unique
labelUnit.setText("Unit"); // set the text for the header
labelUnit.setTextSize(18);
labelUnit.setTextColor(Color.BLACK); // set the color
labelUnit.setPadding(5, 5, 5, 5); // set the padding (if required)
tr_head.addView(labelUnit); // add the column to the table row
TextView labelMinimum = new TextView(this);
labelMinimum.setId(21);// define id that must be unique
labelMinimum.setText("Minimum"); // set the text for the header
labelMinimum.setTextSize(18);
labelMinimum.setTextColor(Color.BLACK); // set the color
labelMinimum.setPadding(5, 5, 5, 5); // set the padding (if required)
tr_head.addView(labelMinimum); // add the column to the table row
TextView labelMaximum = new TextView(this);
labelMaximum.setId(21);// define id that must be unique
labelMaximum.setText("Maximum"); // set the text for the header
labelMaximum.setTextSize(18);
labelMaximum.setTextColor(Color.BLACK); // set the color
labelMaximum.setPadding(5, 5, 5, 5); // set the padding (if required)
tr_head.addView(labelMaximum); // add the column to the table row
TextView labelAccuracy = new TextView(this);
labelAccuracy.setId(21);// define id that must be unique
labelAccuracy.setText("Accuracy"); // set the text for the header
labelAccuracy.setTextSize(18);
labelAccuracy.setTextColor(Color.BLACK); // set the color
labelAccuracy.setPadding(5, 5, 5, 5); // set the padding (if required)
tr_head.addView(labelAccuracy); // add the column to the table row
TextView labelSensorType = new TextView(this);
labelSensorType.setId(21);// define id that must be unique
labelSensorType.setText("SensorType"); // set the text for the header
labelSensorType.setTextSize(18);
labelSensorType.setTextColor(Color.BLACK); // set the color
labelSensorType.setPadding(5, 5, 5, 5); // set the padding (if required)
tr_head.addView(labelSensorType); // add the column to the table row
TextView labelRegisterAddress = new TextView(this);
labelRegisterAddress.setId(21);// define id that must be unique
labelRegisterAddress.setText("RegisterAddress"); // set the text for the
// header
labelRegisterAddress.setTextSize(18);
labelRegisterAddress.setTextColor(Color.BLACK); // set the color
labelRegisterAddress.setPadding(5, 5, 5, 5); // set the padding (if
// required)
tr_head.addView(labelRegisterAddress); // add the column to the table
// row
TextView labelOffset = new TextView(this);
labelOffset.setId(21);// define id that must be unique
labelOffset.setText("Offset"); // set the text for the header
labelOffset.setTextSize(18);
labelOffset.setTextColor(Color.BLACK); // set the color
labelOffset.setPadding(5, 5, 5, 5); // set the padding (if required)
tr_head.addView(labelOffset); // add the column to the table row
TextView labelTimeStamp = new TextView(this);
labelTimeStamp.setId(21);// define id that must be unique
labelTimeStamp.setText("TimeStamp"); // set the text for the header
labelTimeStamp.setTextSize(18);
labelTimeStamp.setTextColor(Color.BLACK); // set the color
labelTimeStamp.setPadding(5, 5, 5, 5); // set the padding (if required)
tr_head.addView(labelTimeStamp); // add the column to the table row
tl.addView(tr_head, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
// fájl megnyitása hogy tudjuk hány id van
try {
File fXmlFile = new File(main.fileWithPath);
DocumentBuilderFactory dbFactory = DocumentBuilderFactory
.newInstance();
DocumentBuilder dBuilder;
dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
doc.getDocumentElement().normalize();
NodeList nList = doc.getElementsByTagName("DAQChannel");
readxml.TablaEddig = nList.getLength();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// fájl vége
Integer count = 0;
for (int i = 0; i < readxml.TablaEddig; i++) {
readxml.read();
// Create the table row
TableRow tr = new TableRow(this);
if (count % 2 != 0)
tr.setBackgroundColor(Color.LTGRAY);
else {
tr.setBackgroundColor(Color.WHITE);
}
tr.setId(100 + count);
tr.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
// Create 5 columns to add as table data
TextView labelid = new TextView(this);
labelid.setId(count);
labelid.setGravity(Gravity.CENTER);
labelid.setText(readxml.getID() + " ");
labelid.setTextColor(Color.BLACK);
tr.addView(labelid);
// Create 5 columns to add as table data
TextView labelname = new TextView(this);
labelname.setId(count);
labelname.setGravity(Gravity.CENTER);
labelname.setText(readxml.getName() + " ");
labelname.setTextColor(Color.BLACK);
tr.addView(labelname);
// Create 5 columns to add as table data
TextView labelunit = new TextView(this);
labelunit.setId(count);
labelunit.setGravity(Gravity.CENTER);
labelunit.setText(readxml.getUnit() + " ");
labelunit.setTextColor(Color.BLACK);
tr.addView(labelunit);
// Create 5 columns to add as table data
TextView labelmin = new TextView(this);
labelmin.setId(count);
labelmin.setGravity(Gravity.CENTER);
labelmin.setText(readxml.getMinimum() + " ");
labelmin.setTextColor(Color.BLACK);
tr.addView(labelmin);
// Create 5 columns to add as table data
TextView labelmax = new TextView(this);
labelmax.setId(count);
labelmax.setGravity(Gravity.CENTER);
labelmax.setText(readxml.getMaximum() + " ");
labelmax.setTextColor(Color.BLACK);
tr.addView(labelmax);
// Create 5 columns to add as table data
TextView labelacc = new TextView(this);
labelacc.setId(count);
labelacc.setGravity(Gravity.CENTER);
labelacc.setText(readxml.getAccuracy() + " ");
labelacc.setTextColor(Color.BLACK);
tr.addView(labelacc);
// Create 5 columns to add as table data
TextView labelst = new TextView(this);
labelst.setId(count);
labelst.setGravity(Gravity.CENTER);
labelst.setText(readxml.getSensorType() + " ");
labelst.setTextColor(Color.BLACK);
tr.addView(labelst);
// Create 5 columns to add as table data
TextView labelra = new TextView(this);
labelra.setId(count);
labelra.setGravity(Gravity.CENTER);
labelra.setText(readxml.getRegisterAddress() + " ");
labelra.setTextColor(Color.BLACK);
tr.addView(labelra);
// Create 5 columns to add as table data
TextView labeloff = new TextView(this);
labeloff.setId(count);
labeloff.setGravity(Gravity.CENTER);
labeloff.setText(readxml.getOffset() + " ");
labeloff.setTextColor(Color.BLACK);
tr.addView(labeloff);
// Create 5 columns to add as table data
TextView labeldate = new TextView(this);
labeldate.setId(count);
labeldate.setGravity(Gravity.CENTER);
labeldate.setText(readxml.getTimeStamp() + " ");
labeldate.setTextColor(Color.BLACK);
tr.addView(labeldate);
// finally add this to the table row
tl.addView(tr, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
count++;
}
}
// ASync Task Begin to perform Billing information
class performBackgroundTask extends AsyncTask<Void, Void, Void> {
private Context context;
private ProgressDialog dialog;
public performBackgroundTask(Context context) {
performBackgroundTask.this.context = context;
}
protected void onPreExecute() {
super.onPreExecute();
dialog = ProgressDialog.show(table.this,
"Feldolgozás",
"Folyamatban...");
}
protected Void doInBackground(Void... params) {
// tableView();
return null;
}
protected void onPostExecute(Void unused) {
if (dialog != null)
dialog.dismiss();
tableView();
Toast.makeText(getApplicationContext(), "Hinnyeeeeeee",
Toast.LENGTH_LONG).show();
}
}
}// oncreate
所以问题是:它第一次没有显示进度对话框,但是当它结束时它会显示一秒钟然后立即隐藏它。
我放入了 tableview() void,它在执行后对 UI 做一些事情。