嗨,我只是想到微调机。我想知道,如果我想将相同的数据粘贴到多个微调器中,我可以用一种方法来做吗?我的意思是将一个适配器连接到许多微调器,或者我必须用其他方法来做?这是简单的例子。假设用户必须从这些微调器中选择三周的时间
public void first() {
List<String> data = db.getBodyParts();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, data);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
firstSpinner.setAdapter(adapter);
firstSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
}
public void second() {
List<String> data = db.getBodyParts();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, data);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
secondSpinner.setAdapter(adapter);
secondSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
}
答案 0 :(得分:0)
您不需要重复的代码,只需将微调器作为参数发送到您的方法中,如下所示:
public void fillSpinner(Spinner spinner) {
List<String> data = db.getBodyParts();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, data);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
}
用法是这样的
fillSpinner(firstSpinner);
或
fillSpinner(secondSpinner);
通过这种方法,您可以使用不限数量的微调器,而无需重复代码。
答案 1 :(得分:-1)
尝试:
public void first() {
List<String> data = db.getBodyParts();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, data);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
firstSpinner.setAdapter(adapter);
firstSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
secondSpinner.setAdapter(adapter);
secondSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
}