2014-02-14 230 views
0

我使用淹沒菜單創建輸入,該菜單使用用戶輸入從服務器獲取數據並將數據添加到列表中。AutoCompleteTextView監聽器

我得到這個代碼:

from_autocompl = (AutoCompleteTextView)rootView.findViewById(R.id.from_autocompl); 
     from_autocompl.addTextChangedListener(new TextWatcher() { 

      @Override 
      public void onTextChanged(CharSequence s, int start, int before, int count) { 
        new JSONParse().execute(); 
        if (ImDoneWithJSON == 1) 
        { 
         ArrayAdapter<List_From_JSON> adapter = new ArrayAdapter<List_From_JSON>(
           this, android.R.layout.simple_list_item_1, data); 
         from_autocompl.setAdapter(adapter); 
         ImDoneWithJSON = 0; 
        } 

      } 
     }); 

現在,我得到的問題是: 我需要知道的是點擊這在下拉列表中的項目。

通常它是通過使用onItemClick做,但我已經有TextWatch聽衆加入到from_autocompl和Android允許在默認情況下只有一個聽衆,現在我想知道如何做到這一點。如何通過這個?

回答

2

您無需使用textChange監聽器使用此,自動完成框有textwatcher

  public class MainActivity extends Activity { 

// private AutoCompleteTextView autoComplete; 
private MultiAutoCompleteTextView multiAutoComplete; 
private ArrayAdapter<String> adapter; 

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

    // get the defined string-array 
    String[] colors = getResources().getStringArray(R.array.colorList); 

    adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,colors); 

    //autoComplete = (AutoCompleteTextView) findViewById(R.id.autoComplete); 
    multiAutoComplete = (MultiAutoCompleteTextView) findViewById(R.id.multiAutoComplete); 

    // set adapter for the auto complete fields 
// autoComplete.setAdapter(adapter); 
    multiAutoComplete.setAdapter(adapter); 

    // specify the minimum type of characters before drop-down list is shown 
    //autoComplete.setThreshold(1); 
    multiAutoComplete.setThreshold(2); 
    // comma to separate the different colors 
    multiAutoComplete.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer()); 

    // when the user clicks an item of the drop-down list 
    multiAutoComplete.setOnItemClickListener(new OnItemClickListener() { 

     @Override 
     public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, 
       long arg3) { 
      Toast.makeText(getBaseContext(), "MultiAutoComplete: " + 
         "you add color "+arg0.getItemAtPosition(arg2), 
         Toast.LENGTH_LONG).show(); 
     } 
    }); 
} 
默認屬性