我正在尝试根据在另一个微调器中选择的项目来更改微调器的值。用于填充两个微调器的值位于一个名为 array.xml 的 xml 文件中。数组文件看起来像
<string-array name="eventYear">
<item>2010</item>
<item>2010</item>
<item>2010</item>
</string-array>
<string-array name="eventName">
<item>something</item>
<item>something</item>
<item>something</item>
<item>something</item>
.
.
.
</string-array>
这是 onCreate 方法和其他相关代码
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_report_selection);
// Display UI Elements
Log.v("RS", "Displaying Headers and Form");
headerText = (TextView) findViewById(R.id.headerTextView);
subheaderText = (TextView) findViewById(R.id.subHeaderTextView);
headerForm = (LinearLayout) findViewById(R.id.headerForm);
// For Event Year
yearTextView = (TextView) findViewById(R.id.yearTextView);
yearSpinner = (Spinner) findViewById(R.id.yearSpinner);
adapter0 = ArrayAdapter.createFromResource(
ReportSelectionActivity.this, R.array.eventYear,
android.R.layout.simple_spinner_dropdown_item);
adapter0.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
yearSpinner.setAdapter(adapter0);
// When a year is selected change the options in the event name spinner
yearSpinner.setOnItemSelectedListener(new myOnItemSelectedListner());
// For Event Names
nameTextView = (TextView) findViewById(R.id.nameTextView);
nameSpinner = (Spinner) findViewById(R.id.nameSpinner);
ArrayAdapter<CharSequence> adapter1 = ArrayAdapter.createFromResource(
ReportSelectionActivity.this, R.array.plzSelect,
android.R.layout.simple_spinner_dropdown_item);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
nameSpinner.setAdapter(adapter1);
// Add table rows to view
yearTableRow = (TableRow) findViewById(R.id.yearTableRow);
nameTableRow = (TableRow) findViewById(R.id.nameTableRow);
}
adapter0 是一个全局变量
private class myOnItemSelectedListner implements OnItemSelectedListener {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
int count = 0;
if (count >= 1) {
// TODO Auto-generated method stub
// Get the year selected from the spinner
int data = Integer.parseInt(yearSpinner.getItemAtPosition(arg2)
.toString());
Log.v("RS", "Data: " + data);
// Depending on which year has been selected intialize the
// new adapter and set it for the event name spinner
switch (data) {
case 2010:
adapter0 = ArrayAdapter.createFromResource(
ReportSelectionActivity.this,
R.array.eventName2010,
android.R.layout.simple_spinner_dropdown_item);
case 2011:
adapter0 = ArrayAdapter.createFromResource(
ReportSelectionActivity.this,
R.array.eventName2011,
android.R.layout.simple_spinner_dropdown_item);
case 2012:
adapter0 = ArrayAdapter.createFromResource(
ReportSelectionActivity.this,
R.array.eventName2012,
android.R.layout.simple_spinner_dropdown_item);
default:
adapter0 = ArrayAdapter.createFromResource(
ReportSelectionActivity.this, R.array.plzSelect,
android.R.layout.simple_spinner_dropdown_item);
}
// Add adapter to spinner
adapter0.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
nameSpinner.setAdapter(adapter0);
// Change UI view
adapter0.notifyDataSetChanged();
}// End of if statment
count++;
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}// End of private class myOnItemSelectedListener
那么为什么 notifyDataSetChanged() 不起作用?
更新代码
private class myOnItemSelectedListner implements OnItemSelectedListener {
ArrayAdapter<CharSequence> adapter;
String[] array;
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// Log.v("RS", "In on onItemSelected");
// TODO Auto-generated method stub
// int count = 0;
/* if (count >= 1) { */
// TODO Auto-generated method stub
// Get the year selected from the spinner
int data;
try{
data = Integer.parseInt(yearSpinner.getItemAtPosition(arg2)
.toString());
}catch(NumberFormatException e){
data = 0;
}
Log.v("RS", "Data: " + data);
// Depending on which year has been selected intialize the
// new adapter and set it for the event name spinner
switch (data) {
case 2010:
Log.v("RS", "Case 2010");
adapter1.clear();
array = getResources().getStringArray(R.array.eventName2010);
list = new ArrayList<String>(Arrays.asList(array));
adapter1.add((CharSequence) list);
/*
* = ArrayAdapter.createFromResource(
* ReportSelectionActivity.this, R.array.eventName2010,
* android.R.layout.simple_spinner_dropdown_item)
*/
;
case 2011:
Log.v("RS", "Case 2011");
adapter1.clear();
array = getResources().getStringArray(R.array.eventName2011);
list = new ArrayList<String>(Arrays.asList(array));
adapter1.add((CharSequence) list);
/*
* adapter0 = ArrayAdapter.createFromResource(
* ReportSelectionActivity.this, R.array.eventName2011,
* android.R.layout.simple_spinner_dropdown_item);
*/
case 2012:
Log.v("RS", "Case 2012");
adapter1.clear();
array = getResources().getStringArray(R.array.eventName2012);
list = new ArrayList<String>(Arrays.asList(array));
adapter1.add((CharSequence) list);
/*
* adapter0 = ArrayAdapter.createFromResource(
* ReportSelectionActivity.this, R.array.eventName2012,
* android.R.layout.simple_spinner_dropdown_item);
*/
default:
Log.v("RS", "Case default");
adapter1.clear();
array = getResources().getStringArray(R.array.plzSelect);
list = new ArrayList<String>(Arrays.asList(array));
adapter1.add((CharSequence) list);
/*
* adapter0 = ArrayAdapter.createFromResource(
* ReportSelectionActivity.this, R.array.plzSelect,
* android.R.layout.simple_spinner_dropdown_item);
*/
}
// Add adapter to spinner
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
nameSpinner.setAdapter(adapter1);
// Change UI view
// Log.v("RS", "notifying view of change");
adapter1.notifyDataSetChanged();
/*
* }// End of if statment
*/
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}// End of private class myOnItemSelectedListener