// spinner Adapter
private ArrayAdapter<String> spinnerAdapter = null;
private Spinner detailSpinner;
private String[] taskStatus = null;
private List<String> statuses = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.spinner_notifydatachanged) ;
taskStatus = new String[]{"a","b","c"} ;
statuses = new ArrayList<String>() ;
statuses.add("a") ;
statuses.add("b") ;
statuses.add("c") ;
detailSpinner = (Spinner)this.findViewById(R.id.statusSel);
spinnerAdapter =new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item,statuses ) ;
spinnerAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
detailSpinner.setAdapter(spinnerAdapter);
Button button = (Button)this.findViewById(R.id.button1) ;
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
taskStatus = null ;
taskStatus= new String[]{"a","b","c","d"} ;
//statuses.add("d") ;
statuses = new ArrayList<String>() ;
statuses.add("1") ;
statuses.add("2") ;
statuses.add("3") ;
statuses.add("4") ;
spinnerAdapter.notifyDataSetChanged() ;
}
}) ;
}
这样的结果:是Spinner 不会刷新的。spinnerAdapter.notifyDataSetChanged() ; 没有效果
原因:@Override
public void onClick(View v) {
taskStatus = null ;
taskStatus= new String[]{"a","b","c","d"} ;
//statuses.add("d") ;
statuses = new ArrayList<String>() ;
statuses.add("1") ;
statuses.add("2") ;
statuses.add("3") ;
statuses.add("4") ;
spinnerAdapter.notifyDataSetChanged() ;
}
红色字体的那就是原因。 这里不能初始化。