需要从我的微调器workRequestType中选择的微调器加载信息。我现在开始在java android中编程,我不知道该怎么做这个程序。
我所做的研究,我需要使用setOnItemSelectedListener但不知道如何使用。我在哪里宣布这个过程?
我的java代码:
spnWorkRequesType.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
String workRequestType = arg0.getItemAtPosition(arg2).toString();
loadCustomServiceSpinner(workRequestType);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}});
private void loadCustomServiceSpinner(String workRequestType) {
CustomServiceDBQueries csQueries = new CustomServiceDBQueries();
customService = csQueries.selectCustomService(workRequestType);
String[] strCustomService = new String[customService.size() + 1];
strCustomService[0] = "";
int i = 1;
for (CustomService cs : customService) {
strCustomService[i] = cs.getCustomServiceName();
i++;
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, strCustomService);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Spinner SpnCustomService = (Spinner) findViewById(R.id.SpnCustomService);
SpnCustomService.setAdapter(adapter);
}
谢谢!
答案 0 :(得分:1)
您可以浏览此spinner tutorial 1和spinner tutorial 2。我认为这会让你对它的工作原理有所了解。
基本上,您需要将一个数组适配器设置为微调器以设置下拉列表。 onItemSelectedListener的onItemSelected方法为您提供从列表中选择的项目的索引。
答案 1 :(得分:0)
我在OnCreate()中声明了该方法并且它有效。谢谢大家的帮助!
spnWorkRequesType
.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int pos, long id) {
String workRequestType = arg0.getItemAtPosition(pos)
.toString();
if (pos != 0)
Toast.makeText(WorkOrderOpen.this, workRequestType,
Toast.LENGTH_LONG).show();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});