我在1个Activity中有2个动态创建的微调器。
private void populateSpinner() {
AlertDialog.Builder adb2 = new AlertDialog.Builder(this);
LayoutInflater adbInflater2 = LayoutInflater.from(this);
View SpinnerLayout = adbInflater2.inflate(R.layout.spinner, null);
adb2.setView(SpinnerLayout);
adb2.setTitle("Kostenstelle auswählen:");
spinnerKOST = (Spinner) SpinnerLayout.findViewById(R.id.spinner);
List<String> lables = new ArrayList<String>();
lables.add("");
spinnerKOST.setSelection(1, false);
for (int i = 0; i < KostList.size(); i++) {
lables.add(KostList.get(i).getKost());
}
ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>
(this,android.R.layout.simple_spinner_dropdown_item, lables);
spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// attaching data adapter to spinner
spinnerKOST.setAdapter(spinnerAdapter);
// use .create to get the AlertDialog
AlertDialog dialog = adb2.create();
// set an OnShowListener
dialog.setOnShowListener(new DialogInterface.OnShowListener() {
public void onShow(DialogInterface dialog) {
spinnerKOST.setOnItemSelectedListener(StaffActivity.this);
}
});
dialog.show();
}
和第二个:
private void populateSpinner2() {
AlertDialog.Builder adb2 = new AlertDialog.Builder(this);
LayoutInflater adbInflater2 = LayoutInflater.from(this);
View SpinnerLayout = adbInflater2.inflate(R.layout.spinner, null);
adb2.setView(SpinnerLayout);
adb2.setTitle("Box auswählen:");
spinnerBox = (Spinner) SpinnerLayout.findViewById(R.id.spinner);
List<String> lables = new ArrayList<String>();
lables.add("");
spinnerBox.setSelection(1, false);
for (int i = 0; i < BoxesList.size(); i++) {
lables.add(BoxesList.get(i).getBoxer_mail());
}
ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>
(this,android.R.layout.simple_spinner_dropdown_item, lables);
spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerBox.setAdapter(spinnerAdapter);
AlertDialog dialog = adb2.create();
dialog.setOnShowListener(new DialogInterface.OnShowListener() {
public void onShow(DialogInterface dialog) {
spinnerBox.setOnItemSelectedListener((OnItemSelectedListener) StaffActivity.this);
}
});
dialog.show();
}
如你所见,它们非常相似。
通常我会使用每个微调器1 public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
之前我每个活动只有1个微调器和1个public void onItemSelected
。
现在我在活动中获得了2个Spinner但仍然是1 public void onItemSelected
。
如何使用两个微调器?
答案 0 :(得分:1)
使用两个微调器和切换视图在它们之间可见和不可见
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Spinner spinner = (Spinner) parent;
if(spinner.getId() == R.id.spinner1)
{
//spinner1
}
else if(spinner.getId() == R.id.spinner2)
{
//spinner2
}
}