I've got to set an autocompletion for my application.
I've already understood the AutoCompleteTextView operation, but I'd like to dynamically modify the String[]
used by android autocompletion.
What I wanna do : call a PHP page which will give me a String[]
that I'll use in my AutoCompleteTextView, but i wanna do that ONLY if a key was pressed at least 500 milliseconds after the last one.
EDIT :
Okay, I was wrong. I want to have my asynctask running if NO KEY is pressed in the 500 milliseconds after the last press (you know, avoiding overcharging our servers by calling a request on every character typed in.)
Here is how I think I'll do :
zipBox.setAdapter(myAdapter); //zipBox is an AutoCompleteTextView
zipBox.addTextChangedListener(new TextWatcher(){
@Override
public void onTextChanged(CharSequence s,
int start, int before, int count){
d = new Date();
GetAutocompletion ac = new GetAutocompletion();
ac.execute(zipBox.getText().toString());
}
// Other methods to implement
});
private class GetAutocompletion extends AsyncTask<String, String, String>{
@Override
protected String doInBackground(String... params){
//adapter.notifyDataSetChanged();
try{
wait(500);
} catch(InterruptedException e){}
Date d1 = new Date();
if(d1.getTime() - d.getTime() > 500){
String res = "";
try{
URLConnection conn = new URL("myUrl.php?term=" + params[0]).openConnection();
InputStream in = conn.getInputStream();
Scanner s = new Scanner(in).useDelimiter("\A");
while (s.hasNext())
res += s.next();
s.close();
in.close();
} catch(Exception e){ Log.d("eduine", "error during autocompletion receive"); }
return json;
} else return null;
}
@Override
protected void onPostExecute(String result){
super.onPostExecute(result);
Log.d("eduine", "json result : " + result);
}
}
What do you think? Is there anyway I could use Timer class?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…