2010-10-17 17 views
7

से वर्तमान सुझाव प्राप्त करना AutoCompleteTextView में वर्तमान शीर्ष सुझाव कैसे प्राप्त करें? मेरे पास यह आइटम सुझा रहा है, और मेरे पास एक टेक्स्ट चेंज श्रोता पंजीकृत है। मेरे पास एक ही स्क्रीन पर एक सूची है। जैसा कि वे टाइप करते हैं, मैं सूची को वर्तमान "सर्वोत्तम" सुझाव में स्क्रॉल करना चाहता हूं। लेकिन मैं यह नहीं समझ सकता कि मौजूदा सुझावों का उपयोग कैसे किया जाए, या कम से कम शीर्ष सुझाव। मुझे लगता है मैं AutoCompleteTextView.getCurrentSuggestions() की तरह कुछ के लिए देख रहा हूँ:`AutoCompleteTextView`

autoCompleteTextView.addTextChangedListener(new TextWatcher() { 
    public void onTextChanged(CharSequence s, int start, int before, int count) { 
      String currentText = autoCompleteTextView.getText(); 
      String bestGuess = autoCompleteTextView.getCurrentSuggestions()[0]; 
      //          ^^^ mewthod doesn't exist 
      doSomethingWithGuess(bestGuess); 
     } 
     public void beforeTextChanged(CharSequence s, int start, int count, 
       int after) { 
      // do nothing 
     } 
     public void afterTextChanged(Editable s) { 
      // do nothing 
     } 
    }); 

उत्तर

15

मेरे द्वारा की गई क्या आप निम्न कोड के साथ क्या करना चाहते हैं:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.autocomplete_1); 

    adapter = new ArrayAdapter<String>(this, 
      android.R.layout.simple_dropdown_item_1line, COUNTRIES); 
    AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.edit); 
    textView.setAdapter(adapter); 

    adapter.registerDataSetObserver(new DataSetObserver() { 
     @Override 
     public void onChanged() { 
      super.onChanged(); 
      Log.d(TAG, "dataset changed"); 
      Object item = adapter.getItem(0); 

      Log.d(TAG, "item.toString "+ item.toString()); 
     } 
    }); 
} 

item.toString पाठ कि पहले आइटम पर प्रदर्शित किया जाता है प्रिंट होगा।

ध्यान दें कि यह तब भी होगा जब आप पॉप-अप (सुझाव) नहीं दिखा रहे हैं। साथ ही, आपको यह जांचना चाहिए कि फिल्टर मानदंड पारित करने वाले कोई आइटम हैं (उपयोगकर्ता का इनपुट उर्फ)।

पहले समस्या को हल करने के लिए:

int dropDownAnchor = textView.getDropDownAnchor(); 
    if(dropDownAnchor==0) { 
     Log.d(TAG, "drop down id = 0"); // popup is not displayed 
     return; 
    } 
    //do stuff 

दूसरी समस्या को हल करने के लिए, का उपयोग getCount> 0

+0

शानदार उत्तर! नीचे मेरे हैक से काफी बेहतर है। –

+0

इसके लिए आपको बहुत बहुत धन्यवाद। मैं फ़िल्टर किए गए आइटमों की संख्या के आधार पर अपने ड्रॉपडाउन की ऊंचाई को गतिशील रूप से बदलने के तरीके के बारे में कुछ दिनों के लिए प्रयास कर रहा था और यह 'आफ्टरटेक्स्ट चेंज' ईवेंट के बाद फ़िल्टर करना प्रतीत होता था। डेटासेट ऑब्सर्वर बिल्कुल श्रोता था जिसे मुझे इसे बनाने के लिए आवश्यक था – Mike

+0

यह एक जगह पर है! –

1

AutoCompleteTextView सबसे अच्छा चयन के लिए नीचे स्क्रॉल नहीं है, लेकिन आप लिखते ही चयन संकीर्णता से। http://developer.android.com/resources/tutorials/views/hello-autocomplete.html

के रूप में मैं इसे देखना AutoCompleteTextView से सुझाव की वर्तमान सूची प्राप्त करने के लिए कोई रास्ता नहीं है: यहाँ यह का एक उदाहरण है।

एकमात्र तरीका ArrayAdapter का कस्टम संस्करण लिख रहा है और इसे AutoCompleteTextView.setAdapter (..) पर भेजता है। यहां source to ArrayAdapter है। ..

भीतरी वर्ग ArrayFilter के क्षेत्र जोड़ें:: आप केवल आंतरिक वर्ग ArrayFilter.performFiltering (में एक विधि में परिवर्तन करना होगा) इतना है कि यह FilterResults को उजागर करता है

public ArrayList<T> lastResults; //add this line 

.. विधि performFiltering के अंत से पहले:

lastResults = (ArrayList<T>) results; // add this line 
    return results; 
} 

इस (लिंक से अनुकूलित उदाहरण) की तरह इसका इस्तेमाल करते हुए:

AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autocomplete_country); 
CustomArrayAdapter<String> adapter = new CustomArrayAdapter<String>(this, R.layout.list_item, COUNTRIES); 
textView.setAdapter(adapter); 

// read suggestions 
ArrayList<String> suggestions = adapter.getFilter().lastResult; 
+0

मैं अपने कोड का परीक्षण, लेकिन में "lastResults = (ArrayList ) परिणाम" लेकिन हम नहीं बदल सकते एक फ़िल्टररसेट्स ArrayList पर टाइप करें। – pengwang

+0

क्या आप मुझे बता सकते हैं कि कैसे संशोधित करें? sdk2.2 – pengwang

+2

कृपया ऊपर दिए गए उत्तर को देखें - यह मेरे से बेहतर समाधान है;) –

संबंधित मुद्दे