我在我的应用程序上有3个旋转器,我必须用Android Studio上的DB (SQLite)的数据填充每个旋转器。
下面是应用程序的渲染:
屏幕截图我的第一个旋转器(Commune, in english: City)被我的DB填充
我的问题:
要填充第二个微调器,我希望根据第一个微调器的选择包含来自数据库的数据。
如何返回我的第一个微调器的值来查询我的DB的第二个微调器的值?
代码:
/**从SQLite数据库加载spinner数据的函数*/
private void loadSpinnerData() {
BddCeos db = new BddCeos(getApplicationContext());
List<String> labelsCommune = db.getAllCommune();
List<String> labelsPoste = db.getAllPoste();
List<String> labelsRef = db.getAllRef();
// Creating adapter for spinner
ArrayAdapter<String> dataAdapterCommune = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, labelsCommune);
ArrayAdapter<String> dataAdapterPoste = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, labelsPoste);
ArrayAdapter<String> dataAdapterRef = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, labelsRef);
// attaching data adapter to spinner
spinner_input_commune.setAdapter(dataAdapterCommune);
spinner_input_nom_poste.setAdapter(dataAdapterPoste);
spinner_input_ref_wapiti.setAdapter(dataAdapterRef);
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// On selecting a spinner item
String Labels = parent.getItemAtPosition(position).toString();
// Showing selected spinner item
Toast.makeText(parent.getContext(), "Vous avez sélectionné : " + Labels, Toast.LENGTH_LONG).show();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
我自己找到了解决方案:
spinner_input_commune.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
BddCeos db = new BddCeos(getApplicationContext());
List<String> labelsPoste = db.getFindPosteByCommune(spinner_input_commune.getSelectedItem().toString());
ArrayAdapter<String> dataAdapterPoste = new ArrayAdapter<String>(Formulaire.this, android.R.layout.simple_spinner_item, labelsPoste);
spinner_input_nom_poste.setAdapter(dataAdapterPoste);
}
@Override
public void onNothingSelected(AdapterView<?> parentView) {
}
});
spinner_input_nom_poste.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
BddCeos db = new BddCeos(getApplicationContext());
List<String> labelsRef = db.getFindRefWapitiByPost(spinner_input_nom_poste.getSelectedItem().toString());
ArrayAdapter<String> dataAdapterRef = new ArrayAdapter<String>(Formulaire.this, android.R.layout.simple_spinner_item, labelsRef);
spinner_input_ref_wapiti.setAdapter(dataAdapterRef);
}
@Override
public void onNothingSelected(AdapterView<?> parentView) {
}
});