textView.getText();
textView.getText().toString(); // If you need an actual String
В настоящее время ваш код не подключает слушателя к текстовому представлению.
Вам необходимо (а) использовать непосредственного слушателя:
textView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
if (adapterView.getItemAtPosition(i).toString().equals("Canada")) {
Toast.makeText(getApplicationContext(), "Result Canada", Toast.LENGTH_SHORT).show();
}
}
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
Или (b) если вы хотите использовать активность в качестве слушателя (я бы не стал), чтобы она реализовала интерфейс и установила itemSelectedListener
на это. Но блин.
Чтобы установить выделенный текст в другой текстовый элемент, необходимо внести несколько изменений. Во-первых, макет должен теперь включать автозаполнение «компонента» и дополнительное текстовое представление. Мы устанавливаем «родительский» макет в вертикальную ориентацию, создаем новый горизонтальный макет для элементов автоматического выбора и добавляем новый текстовый вид.
<LinearLayout xmlns:a="http://schemas.android.com/apk/res/android"
a:orientation="vertical"
a:layout_width="fill_parent"
a:layout_height="wrap_content"
a:padding="5dp">
<LinearLayout a:orientation="horizontal"
a:layout_width="fill_parent"
a:layout_height="wrap_content">
<TextView a:layout_width="wrap_content"
a:layout_height="wrap_content"
a:text="Country"/>
<AutoCompleteTextView a:id="@+id/autocomplete_country"
a:layout_width="fill_parent"
a:layout_height="wrap_content"
a:layout_marginLeft="5dp"/>
</LinearLayout>
<TextView a:id="@+id/selected_country"
a:layout_height="wrap_content"
a:layout_width="fill_parent"/>
</LinearLayout>
Действие получает новое свойство экземпляра TextView
, которое я называю selectedCountry
. Я не показываю свою декларацию. Метод onCreate
ищет его по идентификатору, а выбранный слушатель просто обновляет его.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Text view for selected country.
selectedCountry = (TextView) findViewById(R.id.selected_country);
textView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
selectedCountry.setText(adapterView.getItemAtPosition(i).toString());
}
public void onNothingSelected(AdapterView<?> adapterView) { }
});
}