Code example for AutoCompleteTextView

Methods: setOnItemClickListeneraddTextChangedListener, setOnClickListener

-1
		 * to update the {@link AutoCompleteTextView}'s completion list, after the millisecond wait 
		 * time defined in {@link R.integer.autocomplete_delay}.  Uses a {@link Timer} to schedule 
		 * calls to {@link AsyncGeocoder}. 
		 * 
		 */ 
		tv.addTextChangedListener(new TextWatcher() {
			 
			private final int threshold = activity.getResources().getInteger(R.integer.completion_threshold);
			private final int delay = activity.getResources().getInteger(R.integer.autocomplete_delay);
			 
			private final Timer timer = new Timer();
			 
			private TimerTask lastTimer;
			 
			@Override 
			public void onTextChanged(CharSequence s, int start, int before, int count) {}
			 
			@Override 
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
			 
			@Override 
			public void afterTextChanged(Editable s) {
				// cancel the last timer no matter what if text changed. 
				if(lastTimer != null) {
					lastTimer.cancel();
				} 
				 
				// we check against threshold twice -- first to schedule the task. 
				if(s.length() > threshold) {
					lastTimer = new TimerTask() {
						 
						@Override 
						public void run() { 
							String locationName = tv.getText().toString();
							 
							// we check against threshold twice -- second to see if we 
							// should still bother calling the geocoder after the timeout 
							if(locationName.length() >= threshold) {
								// use the text as it exists when this is called, instead 
								// of the {@link Editable} <code>s</code> 
								geocoder.lookup(tv.getText().toString(), listener, map);
							} 
						} 
					}; 
					// only call the geocoder if a certain amount of time has elapsed. 
					timer.schedule(lastTimer, delay);
				}				 
			} 
		}); 
		 
		/* 
		 * Add a listener that ensures that when one of the auto-fill items is clicked, we jump right away. 
		 */ 
		tv.setOnItemClickListener(new OnItemClickListener() {
			 
			/** 
			 * Jump to clicked address. 
			 */ 
			@Override 
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				lastClickedItem = (BartlebyAddress) parent.getItemAtPosition(position);
				map.getController().animateTo(lastClickedItem.getGeoPoint());
				overlay.addItem(lastClickedItem);
			} 
		}); 
		 
		/* 
		 * Clear text when clicked. 
		 */ 
		 
		/* 
		 * Select all text if clicked when focus changed, do nothing otherwise. 
		 */ 
		tv.setOnClickListener(new OnClickListener() {
			 
			@Override 
			public void onClick(View v) {
				//tv.showDropDown(); 
				//tv.setText("");