I am writing a search function to access a library website.And when the query string is submitted,program launches a new thread to parse infos on the web. it runs normally on AVD but my HTC DesireHD displayed the search results repeatedly,(if the real results are 1. 2. 3. it would appears to be 1. 2. 3. 1. 2. 3.). I set breakpoints at the onQueryTextSubmit method ,found that codes in method onQueryTextSubmit() were executed twice. and here is my code:
sv.setOnQueryTextListener(new OnQueryTextListener(){
@Override
public boolean onQueryTextChange(String newText) {
return false;
}
@Override
public boolean onQueryTextSubmit(String query) {
list.clear();
String str = null;
try {//encoding Chinese character
str = new String(query
.trim().getBytes(), "ISO-8859-1");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
SearchPost sp = new SearchPost(SEARCH_URL + str);
new Thread(sp).start();
return false;
}
});
protected class SearchPost implements Runnable{
public String url = "";
SearchPost(String urls){
url = urls;
}
public SearchPost() {
}
@Override
public void run() {
Message message = handler.obtainMessage();
message.what = DOWNLOAD_COMPLETE;
try{
doc = Jsoup.connect(url).get();
handler.sendMessage(message);
}catch(IOException e){
e.printStackTrace();
message.what = DOWNLOAD_FAIL;
handler.sendMessage(message);
}
}
}
Clearing focus on the SearchView helped me.
sv.clearFocus();
Note: This hides the keyboard as well.
As the problem is that user presses search key on keyboard produces two key-event ACTION_DOWN and ACTION_UP,and some device would react to both these two messages.
I solved in a simple way, as I do not like the setIconified(), because, then searched text is erased, I just let one search per second, so have done this:
public boolean onQueryTextSubmit(String s) {
long actualSearchTime = (Calendar.getInstance()).getTimeInMillis();
// Only one search every second to avoid key-down & key-up
if (actualSearchTime > lastSearchTime + 1000)
{
lastSearchTime=actualSearchTime;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With