android - v.getTag() 返回 null 而不是 ViewHolder

标签 android android-arrayadapter

我有一个自定义的适配器,它有一个标题和自定义的行。有时我的 v.getTag() 在我存储 ViewHolder 的位置返回 null。它不会一直发生,我无法弄清楚它何时以及为什么会发生。

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;

    //Header
    if(items.hasDescription() && 0 == position) {
        LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = vi.inflate(R.layout.app_list_header, null); 
        ((TextView) v.findViewById(R.id.app_list_header_description_text)).setText(items.getDescription());
        return v;
    }

    ViewHolder holder;
    // Inflate app view.
    if (v == null || v.getTag() == null) {
        LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = vi.inflate(textViewResourceId, null); //TODO: parent instead of null?
        holder = new ViewHolder();
        holder.title = (TextView) v.findViewById(R.id.title);
        holder.company = (TextView) v.findViewById(R.id.company);
        holder.priceOrStatus = (TextView) v.findViewById(R.id.price);
        holder.rating = (RatingBar) v.findViewById(R.id.rating);
        holder.icon = (ImageView) v.findViewById(R.id.icon);
        v.setTag(holder);
    } else {
        holder = (ViewHolder) v.getTag();
    }

}
    App app;
    if(items.hasDescription()) {
        app = items.get(position-1);    
    } else {
        app = items.get(position);
    }

    // TODO: Do we need this?
    if (null == app || null == holder) {
        Log.d(TAG, "app: " +app +" holder: " +holder);
        return v;
    }

    //TODO: FIX THE XML BEFORE SO WE DO NOT NEED TO TRIM IT.
    // And get rid of all these ifs!!
    if(holder.title != null) {
        holder.title.setText(app.getTitle().trim());                            

    }

谁能帮帮我?

最佳答案

您在此处使用自定义 ListAdapter 的标准模式。并非所有 View 都会被回收,例如首次创建它们以填充 ListView

您可能还想在创建适配器时引用 LayoutInflater 以稍微提高效率,请参见下面的代码段

 private class AlertListAdapter extends ArrayAdapter< Alert >
 {
        private ViewHolder     holder;
        private LayoutInflater mInflater;

        public AlertListAdapter( Context context, List< Alert > items )
        {
            super( context, R.layout.dashboard_layout, items );
            mInflater = LayoutInflater.from( context );
        }

        public View getView( int position, View recycledView, ViewGroup parent )
        {
            if ( recycledView == null || recycledView.getTag() == null )
            {
                recycledView = mInflater.inflate( R.layout.list_item, null );

                holder = new ViewHolder();                
                holder.header = ( LinearLayout ) recycledView.findViewById( R.id.alert_list_item_header );
                holder.header_text = ( TextView ) recycledView.findViewById( R.id.alert_list_item_header_text );
                holder.header_count = ( TextView ) recycledView.findViewById( R.id.alert_list_item_header_count );
                holder.name = ( TextView ) recycledView.findViewById( R.id.alert_list_item_name );
                holder.distance = ( TextView ) recycledView.findViewById( R.id.alert_list_item_distance );

                recycledView.setTag( holder );
            }
            else
            {
                holder = ( ViewHolder ) recycledView.getTag();
            }

                holder.header_text.setText( title.substring( 0, space ) );
                holder.name.setText( title.substring( space + 1 ) );
                holder.header_count.setText( count );    
                holder.header.setBackgroundResource( resourceID );    

            return recycledView;
        }
    }

基本上,您必须始终准备好让 v.getTag() 返回 null 并相应地扩充新的 View

关于android - v.getTag() 返回 null 而不是 ViewHolder,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6787329/

相关文章:

java - Android 无法更新 ArrayAdapter,因为 "Non-Static method ' notifyDataSetChanged( )' cannot be referenced from a static context"

android - 不兼容类型 : Mainactivity cannot be converted to OnClickListener

android - 我无法将编译 "com.google.firebase:firebase-messaging:9.0.0"应用到我的 build.gradle 文件中

android - RxJava + 改造,获取列表并为每个项目添加额外信息

android - ListView 错误和每部手机的行为不同

android - 使用 weakReference 和 WakHasMap Android 位图

android - 在 Eclipse 中,如何从使用它的项目源中快速访问 "android library project"的源?

android - 从重定向到 HTTPS 连接的 HTTP 连接下载文件

android - 如何在更新数据时更新 ListView ?

android - 我如何在 Android 中填充我的 ListView