2012-07-14 54 views
0

我正在開發一個Android應用程序,當應用程序從Web服務中獲取一些數據時,它會在一段時間內顯示一個空白屏幕。我怎樣才能防止這種情況發生?我會很感激幫助。如何增加android應用程序的響應時間?

protected void onListItemClick(ListView l, View v, final int position, 
     long id) { 
    super.onListItemClick(l, v, position, id); 

    progressDialog = ProgressDialog.show(ProjectListActivity.this, 
      "Please wait...", "Loading..."); 

    new Thread() { 

     public void run() { 

      try { 
       String project = titles.get(position - 1); 

       performBackgroundProcess(project); 

      } catch (Exception e) { 

       Log.e("tag", e.getMessage()); 

      } 

      progressDialog.dismiss(); 
     } 

    }.start(); 





private void performBackgroundProcess(String project) { 

    String spaceId = null; 
    String spaceName = null; 
    /* 
    * for (Space space : spaces){ 
    * if(space.getName().equalsIgnoreCase((String) ((TextView) 
    * v).getText())){ spaceId = space.getId(); } } 
    */ 
    for (Space space : spaces) { 

     if (project.equals(space.getName())) { 

      newSpace = space; 
     } 

    } 

    spaceId = newSpace.getId(); 
    spaceName = newSpace.getName(); 

    /* 
    * Intent intent = new Intent(this, SpaceComponentsActivity.class); 
    * intent.putExtra("spaceId", spaceId); intent.putExtra("tabId", 0); 
    * intent.putExtra("className", "TicketListActivity"); TabSettings ts = 
    * new TabSettings(); ts.setSelTab(1); this.startActivity(intent); 
    */ 
    Intent intent = new Intent(this, SpaceComponentsActivity.class); 
    intent.putExtra("spaceId", spaceId); 
    intent.putExtra("tabId", 0); 
    intent.putExtra("spaceName", spaceName); 

    // intent.putExtra("className", "TicketListActivity"); 
    TabSettings ts = new TabSettings(); 
    ts.setSelTab(0); 
    ts.setSelTabClass("TicketListActivity"); 
    this.startActivity(intent); 
+0

[見這裏...(http://samir-mangroliya.blogspot.in/p/android-customized-listview.html) – 2012-07-14 09:07:27

回答

1

這意味着你正在UI線程上運行網絡相關的操作。您應該考慮使用AsyncTask<?, ?, ?>來代替,以便在網絡線程中運行這些操作以防止UI被鎖定。

例子:

@Override 
public void onResume() { 
    super.onResume(); 
    new MyAsyncTask().execute(); 
} 

private class MyAsyncTask extends AsyncTask<Void, Void, Void> { 

    @Override 
    protected Void doInBackground(Void... params) { 

     // Do your network operations here 

    } 

    @Override 
    protected void onPostExecute(Void result) { 

     // Add items to your ListView here 


    } 

} 
+0

我也做這樣的,但進度對話框在響應之前停止並顯示空白屏幕。 – 2012-07-14 10:23:27

+0

然後你需要向我們展示你的代碼。 :) – ninetwozero 2012-07-14 10:33:45

+0

代碼添加上面。 – 2012-07-14 10:40:59

相關問題