要根据两个Spinner的选择填充ListView,你可以按照以下步骤进行操作:
Spinner spinner1 = findViewById(R.id.spinner1);
Spinner spinner2 = findViewById(R.id.spinner2);
ListView listView = findViewById(R.id.listView);
ArrayAdapter adapter1 = ArrayAdapter.createFromResource(this, R.array.spinner1_items, android.R.layout.simple_spinner_item);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(adapter1);
ArrayAdapter adapter2 = ArrayAdapter.createFromResource(this, R.array.spinner2_items, android.R.layout.simple_spinner_item);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(adapter2);
ArrayAdapter listViewAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1);
listView.setAdapter(listViewAdapter);
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView> parent, View view, int position, long id) {
updateListView();
}
@Override
public void onNothingSelected(AdapterView> parent) {
}
});
spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView> parent, View view, int position, long id) {
updateListView();
}
@Override
public void onNothingSelected(AdapterView> parent) {
}
});
private void updateListView() {
String selectedValue1 = spinner1.getSelectedItem().toString();
String selectedValue2 = spinner2.getSelectedItem().toString();
// 根据选择的值从JSON数据中获取相应的数据
List listViewData = getDataFromJson(selectedValue1, selectedValue2);
// 更新ListView的数据
listViewAdapter.clear();
listViewAdapter.addAll(listViewData);
listViewAdapter.notifyDataSetChanged();
}
getDataFromJson()
方法中,根据两个Spinner的选择从JSON数据中获取相应的数据,并返回一个List这样,当用户选择Spinner的项时,ListView中的数据将根据选择的项自动更新。你需要根据你的具体需求修改代码中的数据获取和更新逻辑。