开发者

Tips for relating spinners

开发者 https://www.devze.com 2023-04-03 22:55 出处:网络
I have a need to add 3 related spinners where the selected content in the first affects what is displayed in the second and then what is selected in the second affects the content in the third. Has an

I have a need to add 3 related spinners where the selected content in the first affects what is displayed in the second and then what is selected in the second affects the content in the third. Has anyone already faced this problem and how did you over come it?

开发者_运维技巧

I was thinking of maintaining a number of arrays in the resources which are numbered and then loaded into the adapter based on the choice selected in the previous spinner. Is this a good way?

Thanks, m


I would have done this with something like that:

spinner1.setOnItemSelectedListener(new OnItemSelectedListener() 
{
    @Override
    public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) 
    {
        SpinnerAdapter adapter = ...create or load the second adapter based on selected item...
        spinner2.setAdapter (adapter);
        spinner3.setAdapter (..create empty adapter...);
    }

    @Override
    public void onNothingSelected(AdapterView<?> parentView) 
    {
        spinner2.setAdapter (..create empty adapter...);
        spinner3.setAdapter (..create empty adapter...);
    }
});

spinner2.setOnItemSelectedListener(new OnItemSelectedListener() 
{
    @Override
    public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) 
    {
        SpinnerAdapter adapter = ...create or load the third adapter based on selected item...
        spinner3.setAdapter (adapter);
    }

    @Override
    public void onNothingSelected(AdapterView<?> parentView) 
    {
        spinner3.setAdapter (..create empty adapter...);
    }
});
0

精彩评论

暂无评论...
验证码 换一张
取 消