活动/意图之间的进度对话

时间:2012-04-23 13:59:18

标签: android android-asynctask

我正在尝试将一个Activity的信息传递给另一个Activity,同时这样做我希望有一个进度对话框显示。主要是当第二个活动正在处理信息时。我一直在阅读,正确的做法似乎是asynctask。还是有另一种方式吗?

这是我的代码:活动一

    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 个答案:

答案 0 :(得分: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();