开发者社区> 问答> 正文

附加图像时 Text View 的 Resource Id 出错

在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
如何解决这个问题?

展开
收起
蛮大人123 2016-06-16 15:31:13 2286 0
1 条回答
写回答
取消 提交回答
  • 我说我不帅他们就打我,还说我虚伪
    /**
    * 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);
    }
    2019-07-17 19:41:02
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
AutoAlerts-From Data to Action 立即下载
低代码开发师(初级)实战教程 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载