【问题标题】:Custom spinner with object ArrayAdapter can't getResources带有对象 ArrayAdapter 的自定义微调器无法获取资源
【发布时间】:2015-11-21 18:48:18
【问题描述】:

我正在使用tutorial here 创建自定义微调器。当我复制课程CountryAdapter(页面的一半)时,eclipse 无法识别getResources() in

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

【问题讨论】:

    标签: android android-arrayadapter android-spinner


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2012-05-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多