2014-10-05 4 views
1

내 응용 프로그램이 데이터베이스 행을 읽고 TextView (또는 복수)이 포함 된 TableRowsTableLayout에 추가합니다. 데이터베이스가 상당히 클 수 있으므로 진행 막대를 구현 한 후 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(); 
}; 

} }

+0

코드 출력이 어떻게됩니까? 충돌합니까? –

+0

@NadeemIqbal 예, 뷰 계층 구조를 생성 한 원래 스레드 ca n 그 견해를 만지십시오. – user3541436

답변

0

이 코드가 도움이 될 것을 확신하지 않는다. 그러나 당신은 또한이 runOnUiThread

@Override 
protected Void doInBackground(Void... params) 
{ 
    runOnUiThread(new Runnable() { 

     @Override 
     public void run() { 
      // TODO Auto-generated method stub 
      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; 
}  
+0

감사합니다. 몇 가지 다른 오류가 발생했지만, 이것은 확실한 것입니다. – user3541436

+1

그렇게하지 마십시오. 전체 doInBackground 메서드가 주 스레드에서 실행되므로 비동기 작업이 수행되는 시점은 무엇입니까? –

0

AsyncTasks를 사용해야 형태로 UI를 업데이트 할 수있는 DoInBackground 방법이라 할 수로 (UI 스레드에서 실행)을 onProgressUpdate.

당신은 당신이 유형은 당신의 작업 AsyncTask<Void, **String**, Void>의 두 번째 유형에 따라 달라집니다 (업데이트를 트리거 및 그 방법에 UI를 수정 표시 할 데이터 publishProgress를 호출해야

예 :.

private class MyTask extends AsyncTask<URL, Integer, Long> { 
protected Long doInBackground(URL... urls) { 
    for (int i = 0; i < 100; i++) { 
     publishProgress(i); 
    } 
    return -1; 
} 

protected void onProgressUpdate(Integer... progress) { 
    setProgressPercent(progress[0]); 
} 
} 

그리고 백그라운드 스레드에서 setContentView(R.layout.clients);을 호출하지 마십시오 ...