在ListViewAdapter中的onClickListener方法内使用holder变量

对于ListView,我制作了持有者对象和所有内容。 如果convertView为null,则初始化它,否则获取标签。

我正在编程上根据一些条件将一个ImageView添加到列表行中。 这也是我加入持有人。

现在视图正在被重用。 所有视图都以ImageView结束。 我怀疑它是因为我动态添加的ImageView。

我如何实现这一点?

提前致谢。

static class ViewHolder{

    TextView MessageText;
    TextView Delivery_Date;
    TextView Time;
    RelativeLayout frame_layout;
    ImageView image_view;
    ProgressBar progress_bar;
} 

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

    View rowView;
    rowView = view;
    pos  = position;
    ViewHolder holder = null;

    if(rowView==null)
        {
            rowView = context.getLayoutInflater().inflate(R.layout.message_row, null);
            holder = new ViewHolder();
            holder.MessageText = (TextView) rowView.findViewById(R.id.message_text);
            holder.Delivery_Date = (TextView) rowView.findViewById(R.id.delivery_date);
            holder.Time = (TextView) rowView.findViewById(R.id.time);
            holder.frame_layout = (RelativeLayout) rowView.findViewById(R.id.frame_layout);
            rowView.setTag (holder);

        }
    else
    {
        /* We recycle a View that already exists */
        holder = (ViewHolder) rowView.getTag();
    }

    holder.image_view = new ImageView(context);
    holder.image_view.setImageResource(R.drawable.ic_action_download);
    holder.image_view.setMinimumHeight(40);
    holder.image_view.setMinimumWidth(40);
    final ProgressBar progress_bar = new ProgressBar(context, null, android.R.attr.progressBarStyleLarge);

    if(// I want to add image condition)
        {
            final ViewHolder final_holder = holder; 
            holder.frame_layout.addView(holder.image_view);
            holder.MessageText.setVisibility(View.GONE);
            if(//I want to add ImageView)
                {

                Bitmap image = assigning image;
                if(image!=null)
                {   holder.image_view.getLayoutParams().width = intendedWidth ;
                    holder.image_view.getLayoutParams().height =    intendedHeight ;
                    holder.image_view.setPadding(10,0,20,0);
                    holder.image_view.setImageBitmap(image);
                }
                else {
                        holder.image_view.setOnClickListener(new OnClickListener(){

                        @Override
                        public void onClick(View arg0) {
                            // TODO Auto-generated method stub
                            final_holder.frame_layout.addView(progress_bar);
                            final_holder.frame_layout.removeView(final_holder.image_view);
                            Download_image download_image = new Download_image(context, MessageList.subscription_db, MessageList.service_id_clicked+"", MessageList.single_service_info.messages.get(position).MessageID);
                            download_image.execute();
                            }
                        });

                    }
                }
            else{
                holder.image_view.setOnClickListener(new OnClickListener(){

                    @Override
                    public void onClick(View arg0) {
                        // TODO Auto-generated method stub
                        final_holder.frame_layout.addView(progress_bar);
                        Download_image download_image = new Download_image(context, MessageList.subscription_db, MessageList.service_id_clicked+"", MessageList.single_service_info.messages.get(position).MessageID);
                        download_image.execute();
                        }
                    });
            }
        }
链接地址: http://www.djcxy.com/p/67343.html

上一篇: Using holder variable inside an onClickListener method in ListViewAdapter

下一篇: Position of items in listview is changing when I scroll the listview