我正在动态填充ArrayList,并添加了一个侦听器来更新带有这些值的微调器。问题是,当我在微调器中选择一个项目时,没有进行任何操作。我按下的项目甚至没有在列表中选中。知道我做错了吗?
主要活动
ArrayAdapter<String> adapterType;
adapterType = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, AvailableHostListString);
hostNameSpinner.setAdapter(adapterType);
hostNameSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapter, View v,int position, long id) {
// On selecting a spinner item
String item = adapter.getItemAtPosition(position).toString();
EditText hostNameEditText = (EditText) findViewById(R.id.hostNameEditText);
hostNameEditText.setText(item);
Toast.makeText(getApplicationContext(), "Detected Spinner Selection", Toast.LENGTH_LONG).show();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
activity_main.xml中
<Spinner
android:id="@+id/hostNameSpinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textAlignment="center" />