在spinner中我想附加一个image 和一个 text View
下面是 layout xml 代码:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<ImageView
android:id="@+id/UICurrencyCurrencyFlag"
android:layout_height="20dp"
android:layout_width="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="@drawable/cross_btn" />
<TextView
android:id="@+id/UICurrencyCurrencyName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:textStyle="bold"
android:layout_toRightOf="@+id/UICurrencyCurrencyFlag"
android:text="TextView" />
</RelativeLayout>
Custom Array Adapter 使用的构造函数:
public class CustomArrayAdapterForCurrencies extends ArrayAdapter<CharSequence>
{
private final Activity context;
public final CharSequence[] keys;
public CustomArrayAdapterForCurrencies(Activity context, CharSequence[] keys)
{
super(context, R.layout.ui_currency_layout,keys);
this.context = context;
this.keys = keys;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = null;
LayoutInflater inflator = context.getLayoutInflater();
view = inflator.inflate(R.layout.ui_currency_layout, null);
final ViewHolder viewHolder = new ViewHolder();
当 activity
开启时,spinner
没有显示 image
和text
,但是当我选择 spinner
时,给出错误 Adapter need resouce id for the text view
如何解决这个问题?
/**
* Constructor
*
* @param context The current context.
* @param textViewResourceId The resource ID for a layout file containing a TextView to use when
* instantiating views.
* @param objects The objects to represent in the ListView.
*/
public ArrayAdapter(Context context, int textViewResourceId, T[] objects) {
init(context, textViewResourceId, 0, Arrays.asList(objects));
}
这是ArrayAdapter的构造方法。
你第二个参数直接传递了一个Layout,而构造方法需要的是一个TextView的Id
这下面是内部代码
...
mFieldId = textViewResourceId;
...
try {
if (mFieldId == 0) {
// If no custom field is assigned, assume the whole resource is a TextView
text = (TextView) view;
} else {
// Otherwise, find the TextView field within the layout
text = (TextView) view.findViewById(mFieldId);
}
} catch (ClassCastException e) {
Log.e("ArrayAdapter", "You must supply a resource ID for a TextView");
throw new IllegalStateException(
"ArrayAdapter requires the resource ID to be a TextView", e);
}
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。