Android应用中的进度对话框问题

时间:2014-03-19 17:55:09

标签: java android

我在试图让进度对话工作的方式上遇到了麻烦。

首先,我在我的可搜索活动类中初始化进度对话框,如下所示:

ProgressDialog pd; 

然后我在我的dosearch方法中执行搜索时显示进度对话框:

public void doSearch(String query) {

    pd = ProgressDialog.show(SearchableActivity.this, "Last Fm", "Searching Tracks...");


    String[] result = new String[] {"searching..."};

    AlbumSearchService service = new AlbumSearchService(query);
    service.addListener(this);
    thread = new Thread(service);
    thread.start();

    setListAdapter(new ArrayAdapter<String> (this ,
                     R.layout.album_list_cell,
                     R.id.text,
                     result));
    Log.v("blah", "doSearch was called");

    service.run();
}

最后,一旦搜索完成,我会在我的服务完整方法中忽略进度指示器。

 pd.dismiss();

我没有看到我做错了什么,每当我开始搜索时,进度对话功能都没有出现请帮助:(

谢谢!

编辑:

这是我的完整代码,适合那些有敏锐眼光的人。我认为这可能是一个线程问题,但我不知道。

公共类SearchableActivity扩展ListActivity实现ServiceListener {     私有线程线程;     private ArrayList searchResults;     ProgressDialog pd;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.search);
    searchResults = new ArrayList<JSONObject>(); // initialise array list to
                                                    // hold the results so
                                                    // we can do something
                                                    // with them

    // this gets the intent being sent over
    Intent intent = getIntent();
    if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
        String query = intent.getStringExtra(SearchManager.QUERY);

        doSearch(query);

    }

}

@Override
// this method handles what happens when a user clicks a result.
// the search item is broadcasted for other people to use.
protected void onListItemClick(ListView l, View v, int position, long id) {
    if (position < searchResults.size()) { // if the position is less than
                                            // the size of the results array
                                            // then...
        Intent result = new Intent(); // create a new intent called result.
        result.setAction(SEARCH_BROADCAST); // place the broadcast message
                                            // on the intent.
        result.putExtra("result", searchResults.get(position).toString()); // add
                                                                            // extended
                                                                            // data
                                                                            // to
                                                                            // the
                                                                            // intent
        this.sendBroadcast(result);

        this.finish();

    }
}

public void doSearch(String query) {

    pd = ProgressDialog.show(SearchableActivity.this, "Last Fm",
            "Searching Tracks...");

    String[] result = new String[] { "searching..." };

    AlbumSearchService service = new AlbumSearchService(query);
    service.addListener(this);
    thread = new Thread(service);
    thread.start();

    setListAdapter(new ArrayAdapter<String>(this, R.layout.album_list_cell,
            R.id.text, result));
    Log.v("blah", "doSearch was called");

    service.run();

}

public void ServiceComplete(AbstractService service) {

    if (pd.isShowing()) {
        pd.dismiss();
    }

    if (!service.hasError()) {
        AlbumSearchService albumService = (AlbumSearchService) service;
        String[] result = new String[albumService.getResults().length()];
        searchResults.clear();

        for (int i = 0; i < albumService.getResults().length(); i++) {
            try {
                searchResults.add(albumService.getResults()
                        .getJSONObject(i)); // gets the json object for each
                                            // result and adds to array
                result[i] = albumService.getResults().getJSONObject(i)
                        .getString("name");
            } catch (JSONException ex) {
                result[i] = "Error";
            }
        }

        setListAdapter(new ArrayAdapter<String>(this,
                R.layout.album_list_cell, R.id.text, result));
    } else {
        String[] result = new String[] { "No Results" };

        setListAdapter(new ArrayAdapter<String>(this,
                R.layout.album_list_cell, R.id.text, result));
    }
}

public static final String SEARCH_BROADCAST = "search_result_selected";

}

0 个答案:

没有答案