Android listview位置序列错误

时间:2012-08-11 01:49:53

标签: android listview

我有大约20个项目的列表视图。每一行适配器都有一个图像按钮,当我点击它时会执行不同的操作。当我点击列表视图的项目时,我设法正确获得位置编号。但是当我点击图像按钮时,我得到的位置是错误的。而不是0~19,但它显示0 1 2 3 0 1 2 3 0 1 2 3 .....

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

    if (convertView == null) {
        convertView     = myInflater.inflate(R.layout.list_video, null);
        holder          = new ViewHolder();

        holder.rlContainer      = (RelativeLayout)  convertView.findViewById(R.id.rlContainer);
        holder.imageView        = (ImageView)       convertView.findViewById(R.id.ivLogo);
        holder.buttonBackground = (ImageView)       convertView.findViewById(R.id.ibButtonLebih);
        holder.tvtitle          = (TextView)        convertView.findViewById(R.id.tvTitle);

        holder.buttonBackground.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {               
                videoListClickListener.OnVideoMoreClickListener(position);
            }
        });

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


    return convertView;
}

1 个答案:

答案 0 :(得分:1)

问题解决了!通过将onclicklistener和setTag移动到if / else之外。

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

if (convertView == null) {
    convertView     = myInflater.inflate(R.layout.list_video, null);
    holder          = new ViewHolder();

    holder.rlContainer      = (RelativeLayout)  convertView.findViewById(R.id.rlContainer);
    holder.imageView        = (ImageView)       convertView.findViewById(R.id.ivLogo);
    holder.buttonBackground = (ImageView)       convertView.findViewById(R.id.ibButtonLebih);
    holder.tvtitle          = (TextView)        convertView.findViewById(R.id.tvTitle);

} else {
    holder = (ViewHolder) convertView.getTag();
}

holder.buttonBackground.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {               
            videoListClickListener.OnVideoMoreClickListener(position);
        }
    });

    convertView.setTag(holder);


return convertView;

}