1

我正在使用此处的教程来创建自定义微调器。当我复制课程(页面中途)时CountryAdapter,eclipse无法识别getResources()

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

有谁知道解决这个问题?基本上,我还能如何获得可绘制对象?我正在复制下面的课程

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;
        }
    }
}
4

1 回答 1

2

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

于 2013-04-18T23:04:46.393 回答