Reputation: 957
Need to load information from a spinner whichever was selected in my spinner workRequestType. I'm starting to program in java android now, and I'm not sure how to do this procedure.
The research I have done, I need to use setOnItemSelectedListener but do not know how to use. Where I declare this process?
My java code:
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);
}
Thank you!
Upvotes: 0
Views: 15364
Reputation: 957
I declared the method in the OnCreate () and it worked. Thank you all for the help!
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
}
});
Upvotes: 2
Reputation:
You may go through this spinner tutorial 1 and spinner tutorial 2. I think it will give you a good idea about how it works.
Basically you need to set an Array adapter to spinner for setting up drop down list. The onItemSelected method of onItemSelectedListener provides you the index of the item you select from the list.
Upvotes: 1