내 응용 프로그램이 데이터베이스 행을 읽고 TextView
(또는 복수)이 포함 된 TableRows
을 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();
};
} }
코드 출력이 어떻게됩니까? 충돌합니까? –
@NadeemIqbal 예, 뷰 계층 구조를 생성 한 원래 스레드 ca n 그 견해를 만지십시오. – user3541436