I am trying to implement the following logic:
User enters a search string in a text box (an activity) -> Web service is called to perform the search asynchronously -> Search results are displayed in a list (another activity)
So far I have the following:
An activity to enter the search string and click a "Search" button:
public class Search extends Activity
{
// ...
// called when "Search" button is clicked
private void runSearch()
{
ProgressDialog progressDialog = ProgressDialog.show(
this, "Search", "Search...");
searchAsyncTask = new SearchAsyncTask();
SearchAsyncTaskParam param = new SearchAsyncTaskParam();
param.SearchString = getSearchCode(); // gets input from text box
param.ProgressDialog = progressDialog;
searchAsyncTask.execute(param);
}
}
Then I have a class to perform the asynchronous search:
public class SearchAsyncTask extends AsyncTask<SearchAsyncTaskParam,
Void, SearchAsyncTaskResult> {
private SearchAsyncTaskParam param;
@Override
protected SearchAsyncTaskResult doInBackground(
SearchAsyncTaskParam... params) {
if (params.length > 0)
param = params[0];
SearchAsyncTaskResult result = new SearchAsyncTaskResult();
// call Webservice and fill result object with status (success/failed)
// and a list of hits (each hit contains name, city, etc.)
return result;
}
@Override
protected void onPostExecute(SearchAsyncTaskResult result) {
param.ProgressDialog.dismiss();
if (!result.Success)
// throw an AlertBox
else
{
// this part is incomplete and doesn't look good, does it?
// And how would I pass my result data to the new activity?
Intent intent = new Intent(param.ProgressDialog.getContext(),
SearchResultList.class);
param.ProgressDialog.getContext().startActivity(intent);
}
}
}
And the last element is an activity to display a list with the search results:
public class SearchResultList extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(
this, android.R.layout.simple_list_item_1, data));
// String was only for testing, should be a class with holds the data
// for each item, i.e. Name, City, etc. And where do I get "data" from?
}
// ...
}
Now my questions:
Is it a good idea to start the activity for the result list in the
onPostExecutemethod of theAsyncTaskinstance? I have tested it with the simple test sketched above and it seems to work. But is this safe, am I on the right thread (UI thread) at all? If this is bad practice what is the alternative to start the result activity?It looks especially weird to use the context of the
ProgressDialogfor theIntentand to start the activity. It is just the only context I have available in theAsyncTaskinstance. Can this be a problem because theProgressDialogis already dismissed? What context else could I use?How do I pass my result data into the
SearchResultListactivity?In case of a successful search (the
elsecase in theonPostExecutemethod above) I would like to close theSearchactivity with the text box so that, if the back button is hit, the user returns to the main screen (which the search activity was opened from) and not theSearchactivity. Is this possible and how can I do that?