Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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 count) {
- 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();
- }
- };
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement