Does it possible to create two "setOnItemSelectedListeners" on Spinner?
I have a problem about onItemSelected method of Spinner. There is no problem if I only have one Spinner. I can set a listener on that. I have a DB stored in sqlite. There is a table in which a column field contains date with year-month-date format. Then I created two Spinner views. One for month, and other for year. My program wants to show the database queries based on selection from month and year.
The problem now, if I set listener only on month Spinner, it shows queries based on month. If I set listener on year Spinner, it shows queries based on year. Actually I already prepared sql queries matching both year and month queries. But it seems that the OnItemSelected listener only can accept "ONE" spinner instance at a time. I try to use monthview.getItemAtPosition(position) && yearview.getItemAtPosition(position) to match both requirements for month and year. But it failed.
I use public class xxx extends xxx implements OnItemSelected listener and add two methods.
spinner.setOnItemSelectedListener(new OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id)
{
// Do some stuff based onItemSelect开发者_开发知识库ed
}
}
Does anyone know how to achieve this goal ?
In simple words, I have two spinners, one for month, one for year, values are populated via arrayadapter. Then I use SimpleCusrsorAdapter to get sql result from sqlite DB. Then I put cursor result into listadapter and associated with listview. It works without problem if I operate it individually either by month or year but not both. But it seems that listener only listen on one spinner instance not two or more at the same time. May be it is common problem. May be some of you already encountered this problem before.
Or can I use anonymous inner class ? But I think that it is different approach with same result. If not may be I need to use another technique if only one listener is available ?
I want to show, for example: -I select April and All years, it displays all entries in April (April in all years) -I select 2010 and All Months, it displays all entries in 2010 (Jan to Dec 2010) -I select April and 2010, it only displays all entries in April 2010
Thanks for explaination and suggestions !!
Tom Best Regards,
Finally I solved this problem by using getSelectedItemPosition() to get the value and do some decision checking. Then program runs without problem.
But I also want to know whether I can use more than two setOnItemSelectedListener for Spinner or other UI component ?
Tom
I am not 100% sure I get your problem.
Look at the method name.
setOnItemSelectedListener
The key here is the set part. Each time you call this method, you override any previously stored values for that object.
But in your cases it seems that what you should do is to call the method once for each of the two spinners either with an anonymous class (as you do there) or with your activity (which will save a bit of memory since you don't have to create the two classes).
As an example:
spinner1.setOnItemSelectedListener(classToReceiveTheSelection)
spinner2.setOnItemSelectedListener(classToReceiveTheSelection)
If you absolutely must have two different classes that respond to the same OnItemSelected, that is possible but somewhat ugly.
精彩评论