自定义微调与对象ArrayAdapter不能getResources [英] Custom spinner with object ArrayAdapter can't getResources
问题描述
我使用的是教程这里创建一个自定义微调。当我复制类 CountryAdapter
(中途下页),月食是无法识别 getResources()
在
myFlag.setBackgroundDrawable(getResources()getDrawable(item.getCountryFlag()));
有谁知道修复这个问题吗?基本上,我怎么回事,可能会得到绘制?我在下面复制
类 公共类CountryAdapter扩展ArrayAdapter< CountryInfo>
{
私人活动范围内;
ArrayList的< CountryInfo>数据= NULL; 公共CountryAdapter(活动方面,诚信资源的ArrayList< CountryInfo>数据)
{
超级(上下文,资源,数据);
this.context =背景;
this.data =数据;
} @覆盖
公共查看getView(INT位置,查看convertView,父母的ViewGroup)
{//在微调普通视图中,我们使用android.R.layout.simple_spinner_item
返回super.getView(位置,convertView,父母);
} @覆盖
公共查看getDropDownView(INT位置,查看convertView,父母的ViewGroup)
{//这种观点开始,当我们单击微调。
查看排= convertView;
如果(行== NULL)
{
LayoutInflater吹气= context.getLayoutInflater();
行= inflater.inflate(R.layout.spinner_layout,父母,假);
} CountryInfo项目= data.get(位置); 如果(项目!= NULL)
{//解析从每个对象中的数据和设置。
ImageView的myFlag =(ImageView的)row.findViewById(R.id.imageIcon);
TextView的myCountry =(TextView中)row.findViewById(R.id.countryName);
如果(myFlag!= NULL)
{
myFlag.setBackgroundDrawable(getResources()getDrawable(item.getCountryFlag()));
}
如果(myCountry!= NULL)
myCountry.setText(item.getCountryName()); } 返回行;
}
}
}
的 getResources()
方法不是在现有的 ArrayAdapter
类,它实际上来自上下文
(在这种情况下,你的活动类,它扩展上下文)。既然你已经到了活动的引用,请尝试使用 context.getResources()
代替。
I am using the tutorial here to create a custom spinner. When I copy the class CountryAdapter
(half-way down the page), eclipse is not able to identify getResources()
in
myFlag.setBackgroundDrawable(getResources().getDrawable(item.getCountryFlag()));
Does anyone know a fix to this problem? Basically, how else might I get the drawable? I am copying the class below
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;
}
}
}
The getResources()
method is not available in the ArrayAdapter
class, it actually comes from the Context
(in this case your Activity class, as it extends Context). Since you've got a reference to the Activity, try using context.getResources()
instead.
这篇关于自定义微调与对象ArrayAdapter不能getResources的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!