无法获得自动填充文字

时间:2014-03-15 16:52:57

标签: android autocompletetextview

我尝试获取我在自动完成中编写的字符串以便稍后使用

AutoCompleteTextView from_txt;
List<String> country_List;
ArrayAdapter<String> adapter;

然后

  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
   AutoCompleteTextView from_txt = (AutoCompleteTextView) findViewById(R.id.from_txt);
    from_txt.setThreshold(1);
 from_txt.addTextChangedListener( this);
    adapter = new ArrayAdapter<String>
 (this,android.R.layout.simple_dropdown_item_1line,country_List);
    from_txt.setAdapter(adapter);
    handler = new Handler();

然后我尝试获取文本

@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {


    Toast.makeText(getApplicationContext(), from_txt.getText(), Toast.LENGTH_LONG).show();  

}

但这不起作用

1 个答案:

答案 0 :(得分:2)

AutoCompleteTextView对象正在定义两次。只需替换:

AutoCompleteTextView from_txt = (AutoCompleteTextView) findViewById(R.id.from_txt);

使用:

from_txt = (AutoCompleteTextView) findViewById(R.id.from_txt);