Advertisement
Guest User

Untitled

a guest
Oct 26th, 2010
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.29 KB | None | 0 0
  1. import org.json.JSONArray;
  2. import org.json.JSONException;
  3. import android.app.Activity;
  4. import android.os.Bundle;
  5. import android.os.Handler;
  6. import android.os.Message;
  7. import android.text.Editable;
  8. import android.text.TextWatcher;
  9. import android.widget.ArrayAdapter;
  10.  
  11. public class YoutubeAutoComplete extends Activity {
  12. Youtube yt = new Youtube();
  13. CustomAutoComplete myAutoComplete;
  14. ArrayAdapter<String> adapter;
  15. private JSONArray js;
  16.  
  17. /** Called when the activity is first created. */
  18. @Override
  19. public void onCreate(Bundle savedInstanceState) {
  20. super.onCreate(savedInstanceState);
  21. setContentView(R.layout.main);
  22.  
  23. super.onCreate(savedInstanceState);
  24.  
  25. setContentView(R.layout.main);
  26.  
  27. myAutoComplete = (CustomAutoComplete) findViewById(R.id.autocomplete);
  28. adapter = new ArrayAdapter<String>(this,
  29. android.R.layout.simple_dropdown_item_1line);
  30. myAutoComplete.addTextChangedListener(textWatcher);
  31. myAutoComplete.setAdapter(adapter);
  32. adapter.notifyDataSetChanged();
  33. }
  34.  
  35. TextWatcher textWatcher = new TextWatcher() {
  36.  
  37. public void onTextChanged(final CharSequence s, int start, int before,
  38. int count) {
  39.  
  40. Thread t = new Thread() {
  41.  
  42. public void run() {
  43. try {
  44. js = yt.GetSuggestions(s.toString()).getJSONArray(1);
  45. messageHandler.sendEmptyMessage(0);
  46. } catch (JSONException e) {
  47. // TODO Auto-generated catch block
  48. e.printStackTrace();
  49. }
  50.  
  51. }
  52. };
  53. t.start();
  54. }
  55.  
  56. public void beforeTextChanged(CharSequence s, int start, int count,
  57. int after) {
  58.  
  59. }
  60.  
  61. public void afterTextChanged(Editable s) {
  62. }
  63. };
  64.  
  65. private Handler messageHandler = new Handler() {
  66. public void handleMessage(Message msg) {
  67. adapter.clear();
  68. for (int i = 0; i < js.length(); i++) {
  69. try {
  70. adapter.add(js.getJSONArray(i).getString(0));
  71. System.out.println(js.getJSONArray(i).getString(0));
  72. } catch (JSONException e) {
  73. // TODO Auto-generated catch block
  74. e.printStackTrace();
  75. }
  76. }
  77. adapter.notifyDataSetChanged();
  78. }
  79. };
  80.  
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement