2012-06-16 37 views
0

我使用文件CSV中的動態表創建簡單的活動。 我想在文件讀取和UI構建之前簡單地顯示進度條。 我堅持在AsyncTask ..如何在顯示progressDialog的asynctask中動態創建表?

如果我執行mytask.get()然後UI建築被阻止,我的期望progressDialog不會顯示。 但是,如果我打電話mytask.get()那麼應用程序崩潰,因爲我的所有UI取決於該CSV。 我發現這一點:

ProgressDialog not shown when AsyncTask.get() called

與相關:

Common class for AsyncTask in Android?

,但我甚至無法想象如何使用這個概念在我的情況..

public void onCreate(Bundle savedInstanceState) { 
      ... 
     Log.i(TAG, "before csv:"); 
     ReadFromCSV mytask = new ReadFromCSV(this); 
     mytask.execute(char_name); 
//  try { 
//   mytask.get(); 
//  } catch (InterruptedException e1) { 
//   // TODO Auto-generated catch block 
//   Log.i(TAG, "1 exception"); 
//   e1.printStackTrace(); 
//  } catch (ExecutionException e1) { 
//   Log.i(TAG, "2 exception"); 
//   e1.printStackTrace(); 
//  } 
     Log.i(TAG, "after csv"); 


     // create table from var headers 
     set_title_with_charname(flipper.get_moves_group(0)); //set activity title 
     Log.i(TAG, "headers values:" + headers); 
     heading.create_header(this, headers, 0); 
     lay.addView(heading); 
     lay.addView(flipper); 
     lay.setOrientation(1); 

     setContentView(lay); 
    } 

這裏是mytask:

private class ReadFromCSV extends AsyncTask<String, Void, String> { 

    public Activity activity; 
    private ProgressDialog Dialog = new ProgressDialog(MovesList.this); 

    public ReadFromCSV(Activity a) { 
     activity = a; 
    } 

    protected void onPreExecute() { 
     Log.i(TAG, "start onPreExecute"); 
     Dialog.setMessage("Please wait..."); 
     Dialog.show(); 

     Log.i(TAG, "END onPreExecute"); 
    } 

    protected String doInBackground(String... urls) { 
     Log.i(TAG, "doInBackground"); 
     // return loadImageFromNetwork(urls[0]); 
     String new_val = urls[0] + "doInBack"; 
     Log.i(TAG, "new_val: " + new_val); 


     try { 
      readfromcsv(flipper, char_name); 

     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     Log.i(TAG, "END doInBackground"); 

     return new_val; 
    } 

    protected void onPostExecute(String result) { 
     Log.i(TAG, "onpostexecute"); 
     Log.i(TAG, "result: " + result); 

     try { 
      if(Dialog.isShowing()) { 
       Dialog.dismiss(); 
      } 
        // do your Display and data setting operation here 
     } 
     catch(Exception e) { 
      Log.i(TAG, "exception: "); 
     } 

     Log.i(TAG, "END onPostExecute"); 
    } 
} 

這裏是readfromcsv的代碼:

public void readfromcsv(MyFlipper flipper, String char_name) 
     throws IOException { 

    String filename = char_name + ".csv"; 
    InputStream is = getAssets().open(filename); 
    BufferedReader in = new BufferedReader(new InputStreamReader(is, 
      "UTF-8")); 

    String reader = ""; 
    int line_nb = 0; 
    MyTable table = null; 
    while ((reader = in.readLine()) != null) { 
     line_nb += 1; 
     Log.d(TAG, "file line: " + reader); 
     String[] row_data = reader.split("ą"); 
     Log.d(TAG, "splitted: " + row_data[0] + ',' + row_data[1] + ".."); 
     if (line_nb == 1) { 
      Log.i(TAG, "first row - memorized headers.."); 
      headers = row_data; 
      Log.i(TAG, "headers memorized: " + row_data[0] + "," 
        + row_data[1] + ".."); 
      continue; 
     } 

     if (row_data[0].equals("")) { 
      // new table 
      // Log.i(TAG, "new moves_group.."); 
      if (table != null) { 

       add_table(table); 
      } 
      Log.d(TAG, "creating new table"); 
      table = new MyTable(this, true, row_data[1]); 
      Log.d(TAG, "new table created"); 
      continue; 
     } 
     Log.d(TAG, "regular row.."); 
     table.row_from_template(this, row_data, line_nb % 2); 
     if (line_nb == 60) { 
      break; 
     } 
     ; 
    } 
    add_table(table); 
    in.close(); 
} 

回答

0

我不知道你的情況下,爲什麼你正在使用獲得儘可能獲得

Waits if necessary for the computation to complete, and then retrieves its result. 

,你可以簡單地做你的UI異步工作使用onPostExecute。

,避免doInBackground和doInBackground調用的函數UI相關wokr ...

0

最有可能要麼你add_table您的一些代碼,您是從doInBackground正在執行一些UI功能調用。由於doInBackground不會在UI線程上發生,因此無法觸及該函數中的UI。相反,您可以使用需要顯示的任何數據調用publishProgress。您可以通過泛型類型聲明來更改publishProgress使用的Type。中間一個是publishProgress使用的。

相關問題