listview的自定义适配器中的ViewHolder在Android中不起作用

时间:2016-02-01 09:42:28

标签: android listview

我是Android的初学者。现在我正在使用自定义适配器的ListView。我读了一篇关于在适配器中使用视图持有者的文章它快了15%。但是当我使用视图持有者时,listview没有显示任何内容。

在使用视图持有者

之前,这是自定义适配器
public class PodcastListAdapter extends ArrayAdapter<Podcast> {
    private ArrayList<Podcast> values;
    private Context context;


    public PodcastListAdapter(Context pcontext,ArrayList<Podcast> pvalues)
    {
        super(pcontext,-1,pvalues);
        this.context = pcontext;
        this.values =pvalues;
    }

    @Override
    public View getView(final int position,View convertView,ViewGroup parent)
    {

        LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rowView = inflater.inflate(R.layout.podcast_list_row,parent,false);
        TextView titleTv = (TextView)rowView.findViewById(R.id.row_title);
        TextView descriptionTv = (TextView)rowView.findViewById(R.id.row_description);
        TextView durationTv = (TextView)rowView.findViewById(R.id.row_duration);
        TextView fileSizeTv = (TextView)rowView.findViewById(R.id.row_file_size);
        titleTv.setText(values.get(position).getTitle());
        descriptionTv.setText(values.get(position).getDescription());
        durationTv.setText(values.get(position).getDuration());
        fileSizeTv.setText(String.valueOf(values.get(position).getFileSize()));
        Log.i("CALLED","CALLED");
        return rowView;
    }

}

它运作良好。

但是当我在适配器中使用这样的视图持有者时

public class PodcastListAdapter extends ArrayAdapter<Podcast> {
    private ArrayList<Podcast> values;
    private Context context;

    static class ViewHolder {
        public TextView titleTextView;
        public TextView descriptionTextView;
        public ImageView imageView;
        public TextView durationTextView;
        public TextView fileSizeTextView;
    }


    public PodcastListAdapter(Context pcontext,ArrayList<Podcast> pvalues)
    {
        super(pcontext,-1,pvalues);
        this.context = pcontext;
        this.values =pvalues;
    }

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

        if(rowView==null)
        {
            LayoutInflater layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            rowView = layoutInflater.inflate(R.layout.podcast_list_row,null);

            ViewHolder viewHolder = new ViewHolder();
            viewHolder.descriptionTextView = (TextView)rowView.findViewById(R.id.row_description);
            viewHolder.titleTextView = (TextView)rowView.findViewById(R.id.row_title);
            viewHolder.durationTextView = (TextView)rowView.findViewById(R.id.row_duration);
            viewHolder.fileSizeTextView = (TextView)rowView.findViewById(R.id.row_file_size);
            viewHolder.imageView = (ImageView)rowView.findViewById(R.id.row_image);
            rowView.setTag(viewHolder);
        }
        ViewHolder holder = (ViewHolder) rowView.getTag();
        holder.titleTextView.setText(values.get(position).getTitle());
        holder.descriptionTextView.setText(values.get(position).getDescription());
        holder.durationTextView.setText(values.get(position).getDuration());
        holder.fileSizeTextView.setText(String.valueOf(values.get(position).getFileSize()));
        return rowView;

    }

}

我的列表视图未显示任何数据。我的代码出了什么问题?

更新

suggested duplicate question与viewHolder无关。

1 个答案:

答案 0 :(得分:0)

因为您正在使用两个不同的Holder对象而您的另一个对象没有任何东西 。在if语句

之外声明你的viewHolder对象

像这样:

@Override
    public View getView(final int position,View convertView,ViewGroup parent)
    {
        View rowView = convertView;
 ViewHolder viewHolder;
        if(rowView==null)
        {
            LayoutInflater layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            rowView = layoutInflater.inflate(R.layout.podcast_list_row,null);
 viewHolder = new ViewHolder();

            viewHolder.descriptionTextView = (TextView)rowView.findViewById(R.id.row_description);
            viewHolder.titleTextView = (TextView)rowView.findViewById(R.id.row_title);
            viewHolder.durationTextView = (TextView)rowView.findViewById(R.id.row_duration);
            viewHolder.fileSizeTextView = (TextView)rowView.findViewById(R.id.row_file_size);
            viewHolder.imageView = (ImageView)rowView.findViewById(R.id.row_image);
            rowView.setTag(viewHolder);
        }
     else {
       viewHolder  = (ViewHolder) rowView.getTag();
    }
        viewHolder.titleTextView.setText(values.get(position).getTitle());
        viewHolder.descriptionTextView.setText(values.get(position).getDescription());
        viewHolder.durationTextView.setText(values.get(position).getDuration());
        viewHolder.fileSizeTextView.setText(String.valueOf(values.get(position).getFileSize()));
        return rowView;

    }