如何在Viewholder设计模式中设置Tag TextView?

时间:2015-01-07 11:30:16

标签: android

我有一个Spinner thad为其Items ...

使用自定义基础适配器

在基本适配器中我使用具有TexTView的视图持有者设计模式..

现在当在getView中inflat TextView并设置标签时,app会抛出classCastException ....

我的代码:

我的自定义基本适配器:

    @Override
public View getView(int position, View view, ViewGroup parent) {
    View convertView = view;
    ViewHolder viewHolder ;
    if (convertView == null) {
        convertView = layoutInflater.inflate(R.layout.spinner_txt, null);
        viewHolder = new ViewHolder();
        viewHolder.textView_title = (TextView) convertView.findViewById(R.id.spinner_txt);
        convertView.setTag(viewHolder);
    }
    else
    {
        viewHolder = (ViewHolder) convertView.getTag();
    }
    viewHolder.textView_title.setTag(cat_ids[position]);
    viewHolder.textView_title.setText(cat_names[position]);
    viewHolder.textView_title.setTypeface(genericTasks.getCustomFont(this.activity, this.activity.getResources().getString(R.string.font1_name)));
    viewHolder.textView_title.setOnTouchListener(this);

    return convertView;
}

我的布局是:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/spinner_txt"
          style="?android:attr/spinnerItemStyle"
          android:singleLine="true"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:ellipsize="marquee"
          android:textSize="@dimen/xlarge_font_size"
          android:textColor="@color/app_black_color"
          android:paddingLeft="@dimen/horizontal_padding_size"
          android:paddingRight="@dimen/horizontal_padding_size"
          android:paddingTop="@dimen/vertical_padding_size"
          android:paddingBottom="@dimen/vertical_padding_size"
          android:layout_centerVertical="true"/>

,例外是:

java.lang.ClassCastException: java.lang.String cannot be cast to co.mobinone.hamamoz.adapters.SpinnerCatsAdapter$ViewHolder
            at co.mobinone.hamamoz.adapters.SpinnerCatsAdapter.getView(SpinnerCatsAdapter.java:65)
            at android.widget.Spinner.measureContentWidth(Spinner.java:772)
            at android.widget.Spinner.onMeasure(Spinner.java:506)
            at android.view.View.measure(View.java:17509)
            at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:719)
            at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:455)
            at android.view.View.measure(View.java:17509)
            at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:719)
            at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:455)
            at android.view.View.measure(View.java:17509)
            at android.widget.ScrollView.measureChildWithMargins(ScrollView.java:1781)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
            at android.widget.ScrollView.onMeasure(ScrollView.java:477)
            at android.view.View.measure(View.java:17509)
            at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:704)
            at android.view.View.measure(View.java:17509)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5380)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
            at android.view.View.measure(View.java:17509)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5380)
            at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:412)
            at android.view.View.measure(View.java:17509)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5380)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
            at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2567)
            at android.view.View.measure(View.java:17509)
            at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2287)
            at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1398)
            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1597)
            at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1256)
            at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6649)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:813)
            at android.view.Choreographer.doCallbacks(Choreographer.java:613)
            at android.view.Choreographer.doFrame(Choreographer.java:583)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:799)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:146)
            at android.app.ActivityThread.main(ActivityThread.java:5653)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
            at dalvik.system.NativeStart.main(Native Method)

感谢。

3 个答案:

答案 0 :(得分:0)

试试这个

@Override
    public View getView(int position, View view, ViewGroup parent) {
        View convertView = view;
        ViewHolder viewHolder ;
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.spinner_txt, null);
            viewHolder = new ViewHolder();
            Textview text = (TextView) convertView.findViewById(R.id.spinner_txt);
             viewHolder.textView_title  = text;
            convertView.setTag(viewHolder);
        }
        else
        {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.textView_title.setTag(cat_ids[position]);
        viewHolder.textView_title.setText(cat_names[position]);
        viewHolder.textView_title.setTypeface(genericTasks.getCustomFont(this.activity, this.activity.getResources().getString(R.string.font1_name)));
        viewHolder.textView_title.setOnTouchListener(this);

        return convertView;
    }    

答案 1 :(得分:0)

基本上你可以将任何类型的对象存储为标记(并在调用getTag时将其强制转换)。这可以是简单的ID或一些复杂的数据。这是您与此视图关联的一些信息。

对于列表和视图持有者模式,它是一个简单的对象,其中包含对标记视图(组)的视图的引用。因此,每次更新视图内容时都不必调用findViewById。这只是一次性能优化。

我们可以在view标签中存储列表项的数据吗? 不会。由于视图回收,您有(例如)10个视图,这些视图可以重复用于1000个列表项。在标签中存储数据在这里没有任何意义。最好使用自定义数据对象来存储列表项状态(可能是包含显示数据的相同数组),或者在列表项更改时立即保留它。

另见setTag documentation.

答案 2 :(得分:0)

您没有发布布局,但假设TextView spinner_txtspinner_txt布局中唯一的元素,此代码

viewHolder.textView_title = (TextView) convertView.findViewById(R.id.spinner_txt);

使textView_titleView相同convertView

viewHolder.textView_title.setTag(cat_ids[position]);

使用字符串覆盖convertView中的viewholder标记。

考虑将cat_ids[position]存储在视图持有者中而不是标记,或使用setTag(int, Object)按键设置其他标记。