myq_26 2013-06-06 02:13 采纳率: 16.7%
浏览 2380

附加图像时 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
如何解决这个问题?

  • 写回答

1条回答

  • 码密码密哄 2013-06-06 04:55
    关注

    /**
    * 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);
    }

    评论

报告相同问题?

悬赏问题

  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥15 MCNP里如何定义多个源?
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 STM32驱动继电器