2 myq 26 myq_26 于 2013.06.06 10:13 提问

附加图像时 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个回答

cj6585256
cj6585256   2013.06.06 12: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);
}

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!