开发者

How to get the spinner id or tag from getDropDownView method in android

I have several spinners that I have created a custom ArrayAdapter for so I can change the drop down menu look. I want to manipulate the view depending on what spinner the dropdown belongs to. I thought I would be able to do something like parent.getTag() but it is returning null.

The custom array adapter looks like:

class BackgroundColorAdapter extends ArrayAdapter<String> {
    BackgroundColorAdapter() {
        super(SettingsActivity.this, R.layout.settings_spinner_item, R.id.item_text, textColors);
    }

    public View getDropDownView (i开发者_如何学JAVAnt position, View convertView, ViewGroup parent){
        View row=super.getView(position, convertView, parent);
        if(parent.getTag().equals("background"){
            //Do custom stuff here
        }
        return(row);
    }
}

and I'm setting the tag:

settingsSpinner.setTag("bg_color_spinner");
settingsSpinner.setAdapter(new BackgroundColorAdapter());

I think I'm confused how the view hierarchy works but it seems logical that the parent of the spinner drop down would be the spinner. Anyone know how I can find out what spinner the drop down belongs to in getDropDownView?

edit: made the settingsSpinner a single spinner instead of an array of spinners to make it less confusing


Eventually got this to work, here is the code for example that changes the text font for each item in the drop down.

class TextSizeAdapter extends ArrayAdapter<String> {

        TextSizeAdapter() {
            super(SettingsActivity.this, R.layout.settings_spinner_item, R.id.item_text, textSizes);
        }

        public View getDropDownView (int position, View convertView, ViewGroup parent){

            View row=super.getView(position, convertView, parent);
            TextView text = (TextView)row.findViewById(R.id.item_text);
            text.setTextSize(TypedValue.COMPLEX_UNIT_PX,appState.FONTSIZES[position]);
            RadioButton radio = (RadioButton)row.findViewById(R.id.item_radio);
            if(settingsSpinners[2].getSelectedItemPosition() == position){
                radio.setChecked(true);
            }else{
                radio.setChecked(false);
            }

            return(row);
        }
    }


I'm unfamiliar with getDropDownView(), and don't know why you use it. Documentation for getDropDownView() states the following about the parent:

parent the parent that this view will eventually be attached to

This doesn't sound like the 'parent' you are looking for... Since the 'parent' in the getView() call is indeed a Spinner, you could use that to store an instance variable of the parent like below:

public Spinner mParent = null;

public View getView (int position, View convertView, ViewGroup parent)
{
    this.mParent = parent;
    return super.getView(position, convertView, parent);
}

public View getDropDownView(int position, View convertView, ViewGroup parent)
{    // Your code here -> but use 'mParent'
}

I haven't tried, but maybe it's a workaround to get what you need. Please let me know if you found the solution.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜