Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
539 views
in Technique[技术] by (71.8m points)

android - Autocompletion delay

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I'd keep a long named lastPress as a field on my TextWatcher. When you press a key, set lastPress = System.currentTimeMillis(). Then just wrap your entire onTextChanged in a if with condition if(System.currentTimeMillis() - lastPress>500) and set lastPress again in that if.


new TextWatcher(){
    long lastPress = 0l;
    @Override
    public void onTextChanged(CharSequence s,
             int start, int before, int count){
        if(System.currentTimeMillis() - lastpress > 500){
            lastPress= System.currentTimeMillis();
            GetAutocompletion ac = new GetAutocompletion();
            ac.execute(zipBox.getText().toString());
        }
    }
    // Other methods to implement
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...