如果我更改或添加了第一个微调器的(spinnerBreeds)值-可以正常工作。但是Second spinner
不会更新值。我尝试在notifyDataSetChanged()
上调用adapter
,但仍然无法正常工作。
我的代码是:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Create DataHelper object and insert some sample data
DataHelper datahelper = new DataHelper(this);
datahelper.insertBreed ("Beagle");
datahelper.insertBreed ("Bulldog");
datahelper.insertBreed ("Chow Chow");
// Get sample data from the database and display them in the spinner
Spinner spinnerBreeds = (Spinner) findViewById(R.id.spinnerBreeds);
ArrayList<String> list = datahelper.getAllBreeds();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.spinner_layout, R.id.text, list);
spinnerBreeds.setAdapter(adapter);
//Init second spinner
DataHelper2 datahelper2 = new DataHelper2(this);
datahelper2.insertAge("1 year");
datahelper2.insertAge ("2-3 years");
datahelper2.insertAge("4-5 years");
datahelper2.insertAge("6-9 years");
datahelper2.insertAge("10+ years");
// Get sample data from the database and display them in the spinner
Spinner spinnerAge = (Spinner) findViewById(R.id.spinnerAge);
ArrayList<String> list2 = datahelper2.getAllAge();
ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this, R.layout.spinner_layout, R.id.text, list2);
spinnerAge.setAdapter(adapter2);
}
最佳答案
Try this code........
spinnerBreeds.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
ArrayList<String> list2 = datahelper2.getAllAge();
ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_item, list2);
spinnerAge.setAdapter(adapter2);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});