2014-02-14 85 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(); 
     } 
    }); 
} 
默认属性