1 /* 2 * Copyright (C) 2008 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.widget; 18 19 import com.android.frameworks.coretests.R; 20 21 import android.app.Activity; 22 import android.os.Bundle; 23 import android.util.Log; 24 import android.view.View; 25 import android.widget.AdapterView.OnItemClickListener; 26 import android.widget.AdapterView.OnItemSelectedListener; 27 28 public class AutoCompleteTextViewSimple extends Activity 29 implements OnItemClickListener, OnItemSelectedListener { 30 31 private final String LOG_TAG = "AutoCompleteTextViewSimple"; 32 33 private AutoCompleteTextView mTextView; 34 35 /** These are cleared by resetItemListeners(), and set by the callback listeners */ 36 public boolean mItemClickCalled; 37 public int mItemClickPosition; 38 public boolean mItemSelectedCalled; 39 public int mItemSelectedPosition; 40 public boolean mNothingSelectedCalled; 41 42 @Override onCreate(Bundle icicle)43 protected void onCreate(Bundle icicle) 44 { 45 // Be sure to call the super class. 46 super.onCreate(icicle); 47 48 // setup layout & views 49 setContentView(R.layout.autocompletetextview_simple); 50 mTextView = findViewById(R.id.autocompletetextview1); 51 52 // configure callbacks used for monitoring 53 mTextView.setOnItemClickListener(this); 54 mTextView.setOnItemSelectedListener(this); 55 resetItemListeners(); 56 57 setStringAdapter(5, "a"); 58 } 59 60 /** 61 * @return The AutoCompleteTextView used in this test activity. 62 */ getTextView()63 public AutoCompleteTextView getTextView() { 64 return mTextView; 65 } 66 67 /** 68 * Set the autocomplete data to an adapter containing 0..n strings with a consistent prefix. 69 */ setStringAdapter(int numSuggestions, String prefix)70 public void setStringAdapter(int numSuggestions, String prefix) { 71 // generate the string array 72 String[] strings = new String[numSuggestions]; 73 for (int i = 0; i < numSuggestions; ++i) { 74 strings[i] = prefix + String.valueOf(i); 75 } 76 77 // install it with an adapter 78 ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 79 android.R.layout.simple_dropdown_item_1line, strings); 80 mTextView.setAdapter(adapter); 81 } 82 83 /** 84 * For monitoring OnItemClickListener & OnItemSelectedListener 85 * 86 * An alternative here would be to provide a set of pass-through callbacks 87 */ resetItemListeners()88 public void resetItemListeners() { 89 mItemClickCalled = false; 90 mItemClickPosition = -1; 91 mItemSelectedCalled = false; 92 mItemSelectedPosition = -1; 93 mNothingSelectedCalled = false; 94 } 95 96 /** 97 * Implements OnItemClickListener 98 */ onItemClick(AdapterView<?> parent, View view, int position, long id)99 public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 100 Log.d(LOG_TAG, "onItemClick() position " + position); 101 mItemClickCalled = true; 102 mItemClickPosition = position; 103 } 104 105 /** 106 * Implements OnItemSelectedListener 107 */ onItemSelected(AdapterView<?> parent, View view, int position, long id)108 public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 109 Log.d(LOG_TAG, "onItemSelected() position " + position); 110 mItemSelectedCalled = true; 111 mItemSelectedPosition = position; 112 } 113 114 /** 115 * Implements OnItemSelectedListener 116 */ onNothingSelected(AdapterView<?> parent)117 public void onNothingSelected(AdapterView<?> parent) { 118 Log.d(LOG_TAG, "onNothingSelected()"); 119 mNothingSelectedCalled = true; 120 } 121 122 } 123