这是我的微调器函数,在活动的OnCreate()中调用初始化微调器和设置适配器数据
private void spinnerSetting() {
DataBaseOperations dataBaseOperations = new DataBaseOperations(this);
spinner = (Spinner) findViewById(R.id.volt_spinner_);
categories = dataBaseOperations.getInvertersType();
// Creating adapter for spinner
dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories);
// Drop down layout style - list view with radio writeNfcBt
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// attaching data adapter to spinner
spinner.setAdapter(dataAdapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
mkvValue = parent.getItemAtPosition(position).toString();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
当我单击按钮时,从这段代码更新我的列表(类别),它向spinner适配器提供数据然后我调用notifyDataSetChange()但它没有微调器值,但我的列表(类别)是已更新并具有新值,但微调器显示旧值
List<Inverters> allData = inverter.getData();
DataBaseOperations dataBaseOperations = new DataBaseOperations(AdminPanelActivity.this);
dataBaseOperations.addInvertersNames(allData);
categories.clear();
//dataAdapter.clear();
categories = dataBaseOperations.getInvertersType();
dataAdapter.notifyDataSetChanged();
spinner.setAdapter(dataAdapter);
答案 0 :(得分:1)
适配器保存对其构造函数中传递的列表的引用。
categories = dataBaseOperations.getInvertersType();
dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories);
dataBaseOperations.getInvertersType()
每次调用时都会创建一个新ArrayList
,但适配器不会更改其内部引用。
这就是为什么您在调用categories = dataBaseOperations.getInvertersType()
后没有看到更改的原因,这是适配器不知道的新列表。
你应该这样做:
List<Inverters> allData = inverter.getData();
DataBaseOperations dataBaseOperations = new DataBaseOperations(AdminPanelActivity.this);
dataBaseOperations.addInvertersNames(allData);
categories.clear();
categories.addAll(dataBaseOperations.getInvertersType());
dataAdapter.notifyDataSetChanged();