我的应用程序正在读取数据库行并将TableRows
与TextView
(或多个)添加到TableLayout
。由于数据库可能很大,因此我需要实施一个进度条,然后执行Async
任务,以便从数据库读取数据并添加到布局中。我现在拥有的东西不起作用,因为我把所有的流程都塞进了doInBackground
。是否有任何元素需要移出异步任务并使其工作?如果不是,我该如何安排它们,以免在执行过程中出现错误?将元素添加到异步任务中的布局
public class orders extends Activity {
private DataBaseManager dataBase;
//put the table name and column in constants
public static final String TABLE_NAME = "clients";
public static final String COLUMN_ID = "id";
public static final String COLUMN_CLIENTS = "code";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LoadData task = new LoadData();
task.execute();
}
public class LoadData extends AsyncTask<Void, Void, Void> {
ProgressDialog progressDialog;
//declare other objects as per your need
@Override
protected void onPreExecute()
{
progressDialog = ProgressDialog.show(YourActivity.this, "Progress Dialog Title Text","Process Description Text", true);
};
@Override
protected Void doInBackground(Void... params)
{
setContentView(R.layout.clients);
TableLayout tl = (TableLayout) findViewById(R.id.clieTable);
newClientButton = (Button)findViewById(R.id.newClientButton);
dataBase = DataBaseManager.instance();
Cursor cursor = dataBase.select("SELECT * FROM " + TABLE_NAME);
while (cursor.moveToNext()) {
int id = cursor.getInt(cursor.getColumnIndex(COLUMN_ID));
TextView textClieCode = new TextView(this);
textClieCode.setLayoutParams(new TableRow.LayoutParams(0, 60, 0.04f));
textClieCode.setBackground(getResources().getDrawable(R.drawable.row_background));
textClieCode.setTextColor(Color.BLACK);
textClieCode.setTextSize(18);
TableRow tr = new TableRow(this);
tr.addView(textClieCode);
tl.addView(tr);
String code = cursor.getString(cursor.getColumnIndex(COLUMN_CLIENTS));
textClieCode.append(" " + code + "\n");
}
}
return null;
}
@Override
protected void onPostExecute(Void result)
{
super.onPostExecute(result);
progressDialog.dismiss();
};
}}
代码的输出是什么?它会崩溃吗? – 2014-10-05 16:31:27
@NadeemIqbal是的,只有创建视图层次结构的原始线程ca ñ触及其观点。 – user3541436 2014-10-05 16:39:05