I have a search function when the user types something in an input and presses enter, an asynctask will fire off to retrieve the results. I want my textview (visibility=gone) which says "Loading" to be displayed before/while the asynctask is running. However, it only displays the loading text for a second, before showing the results in postExecute. So during the few seconds the asynctask is running, I do not see a loading text. Does anyone know why?
As you can see in my code, I have set the loading text to be visible before the asynctask runs and during preExecute in the asynctask, and neither solves the problem.
edit is my EditText, retrieving is my "loading" TextView
Here is my EditText keylistener:
edit.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (keyCode) {
case KeyEvent.KEYCODE_ENTER:
retrieving.setVisibility(View.VISIBLE);
SearchTask task = (SearchTask) new SearchTask(edit,retrieving);
task.execute(edit.getText().toString());
try {
task.get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
break;
}
}
return false;
}
});
Here is the asynctask:
public class SearchTask extends AsyncTask {
private EditText edit;
private TextView retrieving;
public SearchTask(EditText edit,TextView retreiving, ) {
this.edit = edit;
this.retrieving = retreiving;
}
@Override
protected void onPreExecute() {
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(edit.getWindowToken(), 0);
retrieving.setVisibility(View.VISIBLE);
}
@Override
protected Playlist doInBackground(String... params) {
//download and parse xml
}
@Override
protected void onPostExecute(Playlist playlist) {
retrieving.setVisibility(View.GONE);
}