public class testAddSpiner extends Activity {
private Spinner m_myDynamicSpinner;
private EditText m_addItemText;
private ArrayAdapter<CharSequence> m_adapterForSpinner;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
m_myDynamicSpinner = (Spinner)findViewById(R.id.dynamicSpinner);
m_addItemText = (EditText)findViewById(R.id.newSpinnerItemText);
Button addButton = (Button)findViewById(R.id.AddBtn);
Button clearButton = (Button)findViewById(R.id.ClearBtn);
m_adapterForSpinner = new ArrayAdapter(this, android.R.layout.simple_spinner_item);
m_adapterForSpinner.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
m_myDynamicSpinner.setAdapter(m_adapterForSpinner);
m_adapterForSpinner.add("gr");
addButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
addNewSpinnerItem();
}
});
clearButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
clearSpinnerItems();
}
});
}
private void addNewSpinnerItem() {
CharSequence textHolder = "" + m_addItemText.getText();
m_adapterForSpinner.add(textHolder);
}
private void clearSpinnerItems() {
m_adapterForSpinner.clear();
m_adapterForSpinner.add("dummy item");
}
}
我的微调器m_adapterForSpinner更新非常好,如果我从m_adapterForSpinner中选择一个,它可以在spinner上显示。我想做的是我添加一个数据,它不仅显示在m_adapterForSpinner上,还显示在微调器上。
答案 0 :(得分:1)
您是否尝试过选择第一项? spinner.setSelection(0);
?
答案 1 :(得分:1)
addButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
addNewSpinnerItem();
int a= m_adapterForSpinner.getCount();
m_myDynamicSpinner.setSelection(a-1);
// m_adapterForSpinner.notifyDataSetChanged();
}
});
答案 2 :(得分:0)
尝试在向适配器添加项目后调用notifyDataSetChanged():
private void addNewSpinnerItem() {
CharSequence textHolder = "" + m_addItemText.getText();
m_adapterForSpinner.add(textHolder);
m_adapterForSpinner.notifyDataSetChanged();
}