Android efficient AsyncTask
I have an EditText where user can type in the name of the business. I also have a ListView below this EditText which suggest user about what is already added to the database...
<EditText android:id="@+id/txtBusinessName" android:hint="Name of Business" />
<ListView android:id="@+id/suggestionList"
android:layout_width="fill_parent" android:layout_height="wrap_content">
</ListView>
Now as user types in, I check for the keyword they typed, in the database and retrieve what it has to show the user in a ListView. Currently on every key up event fired, I am calling a new AsyncTask this way...
EditText txtBusinessName = (EditText) findViewById(R.id.txtBusinessName);
txtBusinessName.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_UP) {
if (v instanceof EditText) {
EditText txtBusinessName = ((EditText) v);
if (txtBusinessName.length() > 0) {
if (suggestionTask != null) {
suggestionTask.cancel(true);
suggestionTask = null;
}
开发者_StackOverflow社区 suggestionTask = new GetCompaniesByKeywordAsyncTask(
AddBusinessActivity.this, s);
suggestionTask.execute(txtBusinessName.getText()
.toString());
}
}
}
return false;
}
});
Is there a way I just have a single instance of AsyncTask and ask it to retrieve the names as user types in the EditText? Because creating too many AsyncTask isn't efficient and will end up in exception. I will populate the ListView upon receiving the names, can I ask ListView to re-size itself based on content inside it?
In order to make a single AsyncTask, you need restructure the AsyncTask to run based on the request queue. The queue is containing all the keywords that you want to process. You would then run this AsyncTask outside of the listener once and add the keyword from the OnKeylistener.
To update the ListView, we will utilize onProgressUpdate that will update the ListView based on the result in doInBackground
The skeleton code for the modification AsyncTask
@Override
protected Integer doInBackground(Void... params) {
int errorCode = 0;
try {
// while running in the context of your activity
// you should set this boolean to false once you have leave the activity
while(!isRunning){
// blocking call to get the next keyword that is added to the queue
String responseData = getNextKeyword();
// once you get the next keyword, you publish the progress
// this would be executed in the UI Thread and basically would update the ListView
publishProgress(responseData);
}
} catch(Exception e) {
// error handling code that assigns appropriate error code
}
return errorCode;
}
@Override
protected void onPostExecute(Integer errorCode) {
// handle error on UI Thread based on errorCode
}
@Override
protected void onProgressUpdate(String... values) {
String searchKeyword = values[0];
// handle the searchKeyword here by updating the listView
}
/***
* Stub code for illustration only
* Get the next keyword from the queue
* @return The next keyword in the BlockingQueue
*/
private String getNextKeyword() {
return null;
}
/***
* Stub code for illustration only
* Add new keyword to the queue, this is called from the onKey method
* @param keyword
*/
public void addKeyword(String keyword) {
// add the keyword to the queue
}
Then your code is rougly modified to:
// instantiate AsyncTask once
suggestionTask = new GetCompaniesByKeywordAsyncTask(
AddBusinessActivity.this, s);
// run only one AsyncTask that is waiting for any keyword in the queue
suggestionTask.execute();
EditText txtBusinessName = (EditText) findViewById(R.id.txtBusinessName);
txtBusinessName.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_UP) {
if (v instanceof EditText) {
EditText txtBusinessName = ((EditText) v);
if (txtBusinessName.length() > 0) {
// add new keyword to the queue for processing
suggestionTask.addKeyword(txtBusinessName.getText()
.toString());
}
}
}
return false;
}
});
精彩评论