2012-04-23 61 views
2

我試圖將信息從一個活動傳遞給另一個活動,同時這樣做我希望有一個進度對話框顯示。主要是當第二項活動正在處理信息時。我一直在閱讀,做這件事的正確方式似乎是異想天開。還是有另一種方式呢?活動/意圖之間的進程對話框

這裏是我的代碼:活動一個

public class SearchActivity extends Activity { 

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.search); 

    final EditText edittext = (EditText) findViewById(R.id.edittext); 
    edittext.setOnKeyListener(new OnKeyListener() { 
     public boolean onKey(View v, int keyCode, KeyEvent event) { 
      // If the event is a key-down event on the "enter" button 
      if ((event.getAction() == KeyEvent.ACTION_DOWN) 
        && (keyCode == KeyEvent.KEYCODE_ENTER)) { 
       // Perform action on key press 
       String query = edittext.getText().toString(); 
       // gets the text and makes sure its a string 
       Intent intent = new Intent(SearchActivity.this, 
         DissertationActivity.class); 
       intent.putExtra("query1", query); 
       startActivity(intent); 

       return true; 
      } 
      return false; 
     } 
    }); 

    final Button button = (Button) findViewById(R.id.searchButton); 
    button.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      String query = edittext.getText().toString(); 
      // gets the text and makes sure its a string 

      Intent intent = new Intent(SearchActivity.this, 
        DissertationActivity.class); 
      intent.putExtra("query1", query); 
      startActivity(intent); 

     } 
    }); 

} 
    } 

這是第二個活動:

 public class DissertationActivity extends ListActivity { 
/** Called when the activity is first created. */ 

public ArrayList<String> book_Array = new ArrayList<String>(); 
ArrayAdapter<String> adapter; 

String href = ""; 
String href1 = ""; 
String search_Word = ""; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 



    Bundle extras = getIntent().getExtras(); 
    search_Word = extras.getString("query1"); 

    adapter = new ArrayAdapter<String>(this, R.layout.list_item_1, 
      book_Array); 
    setListAdapter(adapter); 

    ListView lv = getListView(); 
    lv.setTextFilterEnabled(true); 

    try { 
     Document doc = null; 
     Document guestLink = null; 

     guestLink = Jsoup.connect("https://aulib.abdn.ac.uk:443/F").get(); 
     Element link = guestLink.select("p > a").first(); 
     href1 = link.attr("href"); 
     href = href1.substring(0, href1.length() - 2); // removes -0 from 
                 // the 

     // href_Array.add(href); //adds href to the array because string 
     // wont add to the public var. 
     doc = Jsoup.connect(
       href + "&request=" + search_Word 
         + "&find_code=WRD&adjacent=N&x=0&y=0").get(); 
     // System.out.println(doc); 
     Elements headings = doc.select("td:eq(3)"); 
     // System.out.println(headings); 
     for (Element heading : headings) { 
      // System.out.println(heading.text()); 
      String j = heading.text(); 

      book_Array.add(j); 

     } 

    } catch (IOException e) { 
     e.printStackTrace(); 

    } 

    book_Array.remove(0); 
    adapter.notifyDataSetChanged(); 
    book_Array.remove(1); 
    adapter.notifyDataSetChanged(); 
    book_Array.remove(2); 
    adapter.notifyDataSetChanged(); 
    book_Array.remove("Search"); 
    adapter.notifyDataSetChanged(); 
    book_Array.remove(" | "); 
    adapter.notifyDataSetChanged(); 
    book_Array.remove(0); 
    adapter.notifyDataSetChanged(); 

    lv.setOnItemClickListener(new OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> a, View v, int position, 
       long id) { 
      // Context context = getApplicationContext(); 
      int query = position; 
      // String text = book_Array.get(position); 
      // int duration = Toast.LENGTH_SHORT; 
      // Toast toast = Toast.makeText(context, 
      // String.valueOf(position), //shows the postion in the array 
      // list 
      // duration); 
      // toast.show(); 

      Intent intent = new Intent(DissertationActivity.this, 
        FullDetailsActivity.class); 
      intent.putExtra("href", href); 
      intent.putExtra("query1", (int) query); 
      intent.putExtra("search_Word", search_Word); 

      startActivity(intent); 
     } 
    }); 

} 

} 

我嘗試使用:

 this.pd = ProgressDialog.show(this, "Working..", "Downloading Data...", 
      true, false); 

但沒有奏效。

我該怎麼辦,以便它在活動之間顯示進度對話框?

感謝您的幫助!

+1

爲什麼不使用AsyncTask?你有三種實現方法: 'onPreExecute' - 顯示進度條; 'onPostExecute' - 刪除進度條; 'doInBackground' - 應該做什麼; – vtuhtan 2012-04-23 14:12:16

+0

只是一個提示,如果你在他們描述的行之前發表評論,你的代碼將更容易閱讀。這樣,在試圖解析頭腦中的語言之前,人們會知道你想要做什麼。 – gobernador 2012-04-23 14:13:31

+0

你在哪裏顯示對話框?問題是對話框沒有顯示? – dreamtale 2012-04-23 14:15:01

回答

0

調用ProgressDialog.show將阻止UI線程。所以在方法返回之前,進度對話框/條不會顯示出來。所以我們可以爲我們的方法在其中運行創建一個線程。這將避免阻塞主UI線程。 示例代碼 -

ProgressDialog spinnerDialog = ProgressDialog.show( 
Placeholder.this, "","Your text ", true); 
new Thread(new Runnable() { 
public void run() { 
    //your method code 
    return; 
} 
}).start(); 
+0

我嘗試使用代碼,但我不確定它是否在正確的位置。它顯示進度對話框,但它會在幾秒鐘後崩潰應用程序。我把 ProgressDialog spinnerDialog = ProgressDialog.show( \t \t \t \t DissertationActivity.this, 「」, 「您的文本」,真正的); \t \t \t \t新主題(新的Runnable(){ \t \t \t \t公共無效的run(){ 剛過線48的setListAdapter是嗎? – Tbuermann 2012-04-23 15:00:31

+0

這應該理想地工作。你也可以添加一個睡眠語句作爲運行方法的第一行,說明您希望顯示進度對話框的時間量。方法代碼將在後面介紹 – 22kar 2012-04-23 15:52:24