我遇到的问题是我希望在选择(例如)加拿大之后填充spinner2
,但出于某种原因,spinner2
会在启动时填充,而不是在我选择加拿大之后填充。我还将一个字符串数组连接到spinner2
以确保它没有任何启动,但它仍然不起作用。以下是我到目前为止的情况:
public class MainActivity extends Activity {
private Spinner spinner1, spinner2;
private Button button;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton();
addListenerOnSpinnerItemSelection();
spinner1 = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.countries,
android.R.layout.simple_list_item_1);
spinner1.setAdapter(adapter);
spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
TextView tv = (TextView)v; // cast the View to a TextView
if ("Canada".equals(tv.getText().toString()));
{
addItemsOnSpinner2();
}
}
public void onNothingSelected(AdapterView<?> arg0) {
}
});
}
// add items into spinner dynamically
public void addItemsOnSpinner2() {
spinner2 = (Spinner) findViewById(R.id.spinner2);
List list = new ArrayList();
list.add("hello");
list.add("holla");
list.add("hey");
list.add("hi");
ArrayAdapter dataAdapter = new ArrayAdapter(this,android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(dataAdapter);
}
public void addListenerOnSpinnerItemSelection() {
spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner2 = (Spinner) findViewById(R.id.spinner2);
spinner1.setOnItemSelectedListener(new Spinner2());
spinner2.setOnItemSelectedListener(new Spinner2());
}
public void addListenerOnButton() {
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,
"Result : " +
"\nSpinner 1 : "+ String.valueOf(spinner1.getSelectedItem()) +
"\nSpinner 2 : "+ String.valueOf(spinner2.getSelectedItem()), Toast.LENGTH_SHORT).show();
}
});
}
}
Spinner2.java *
public class Spinner2 implements OnItemSelectedListener {
@Override
public void onItemSelected(AdapterView parent, View view, int pos, long id) {
Toast.makeText(parent.getContext(),
parent.getItemAtPosition(pos).toString(),
Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView parent) {
}
}
答案 0 :(得分:0)
这是交易..在刚刚创建Spinner时仍然会调用onItemSelected
方法而不选择任何选项。一个简单的解决方法是在spinner liek“Select”或“None”中添加一个额外选项,并在if-else
方法中使用onItemSelected
构造,如下所示: -
@Override
public void onItemSelected(AdapterView parent, View view, int pos, long id) {
if(pos==0) {
//do something;
} else {
Toast.makeText(parent.getContext(),
parent.getItemAtPosition(pos).toString(),
Toast.LENGTH_SHORT).show();
}
}
答案 1 :(得分:0)
正如@ d3m0li5h3r正确提及
当Spinner刚刚初始化时仍然会调用onItemSelected方法
所以,这是一个解决方法,而不是在微调器中添加额外的元素,..
spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
boolean check = false;
public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
if(count){
TextView tv = (TextView)v; // cast the View to a TextView
if ("Canada".equals(tv.getText().toString()));
{
addItemsOnSpinner2();
}
}
check = true;
}
public void onNothingSelected(AdapterView<?> arg0) {
}
});