开发者

Android dynamic auto-complete - minor fix - code attached

开发者 https://www.devze.com 2023-01-20 15:20 出处:网络
I am trying to implement a dynamic autocomplete widget in android. I am done with the major functionalities and the autocompletion implemented is for youtube video search.

I am trying to implement a dynamic autocomplete widget in android. I am done with the major functionalities and the autocompletion implemented is for youtube video search.

When I start typing a letter or two, the auto-completion is not working. But when I type three letters or more it works prfect. It also works when I type two letters and hit a backspace. I do not know what is wrong with the code.

I have uploaded the code here

Experts, kindly guide me. I would be obliged if you can point out where I have gone wrong with the code.

Any help in this regard is well appreciated.

Looking forward, Regards, Rony

import org.json.JSONArray;
import org.json.JSONException;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.ArrayAdapter;

public class YoutubeAutoComplete extends Activity {
Youtube yt = new Youtube();
CustomAutoComplete myAutoComplete;
ArrayAdapter<String> adapter;
private JSONArray js;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    myAutoComplete = (CustomAutoComplete) findViewById(R.id.autocomplete);
    adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_dropdown_item_1line);
    myAutoComplete.addTextChangedListener(textWatcher);
    myAutoComplete.setAdapter(adapter);
    adapter.notifyDataSetChanged();
}

TextWatcher textWatcher = new TextWatcher() {

    public void onTextChanged(final CharSequence s, int start, int before,
            int cou开发者_开发问答nt) {

        Thread t = new Thread() {

            public void run() {
                try {
                    js = yt.GetSuggestions(s.toString()).getJSONArray(1);
                    messageHandler.sendEmptyMessage(0);
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }
        };
        t.start();
    }

    public void beforeTextChanged(CharSequence s, int start, int count,
            int after) {

    }

    public void afterTextChanged(Editable s) {
    }
};

private Handler messageHandler = new Handler() {
    public void handleMessage(Message msg) {
        adapter.clear();
        for (int i = 0; i < js.length(); i++) {
            try {
                adapter.add(js.getJSONArray(i).getString(0));
                System.out.println(js.getJSONArray(i).getString(0));
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        adapter.notifyDataSetChanged();
    }
};

}


Take a look at completionThreshold AKA the number of characters the user must type before getting completion suggestions.

http://developer.android.com/reference/android/widget/AutoCompleteTextView.html#attr_android:completionThreshold

0

精彩评论

暂无评论...
验证码 换一张
取 消