具有对象ArrayAdapter的自定义微调器无法获取getResources

时间:2013-04-18 22:57:40

标签: android android-arrayadapter android-spinner

我使用tutorial here创建自定义微调器。当我复制班级CountryAdapter(页面的一半)时,eclipse无法识别<{p>}中的getResources()

myFlag.setBackgroundDrawable(getResources().getDrawable(item.getCountryFlag()));

有没有人知道这个问题的解决方法?基本上,我怎么可能得到drawable?我正在复制下面的课程

public class CountryAdapter extends ArrayAdapter<CountryInfo>
    {
        private Activity context;
        ArrayList<CountryInfo> data = null;

        public CountryAdapter(Activity context, int resource, ArrayList<CountryInfo> data)
        {
            super(context, resource, data);
            this.context = context;
            this.data = data;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) 
        {   // Ordinary view in Spinner, we use android.R.layout.simple_spinner_item
            return super.getView(position, convertView, parent);   
        }

        @Override
        public View getDropDownView(int position, View convertView, ViewGroup parent)
        {   // This view starts when we click the spinner.
            View row = convertView;
            if(row == null)
            {
                LayoutInflater inflater = context.getLayoutInflater();
                row = inflater.inflate(R.layout.spinner_layout, parent, false);
            }

            CountryInfo item = data.get(position);

            if(item != null)
            {   // Parse the data from each object and set it.
                ImageView myFlag = (ImageView) row.findViewById(R.id.imageIcon);
                TextView myCountry = (TextView) row.findViewById(R.id.countryName);
                if(myFlag != null)
                {
                    myFlag.setBackgroundDrawable(getResources().getDrawable(item.getCountryFlag()));
                }
                if(myCountry != null)
                    myCountry.setText(item.getCountryName());

            }

            return row;
        }
    }
}

1 个答案:

答案 0 :(得分:2)

getResources()类中没有ArrayAdapter方法,它实际上来自Context(在本例中是您的Activity类,因为它扩展了Context)。由于您已获得对活动的引用,请尝试使用context.getResources()