Using holder variable inside an onClickListener method in ListViewAdapter

For the ListView I have made the holder object and everything. Initializing it if the convertView is null, else getting the tag.

I am programtically adding an ImageView to the List Row based on some conditions. That also I am adding in the holder.

Now the views are getting re-used. And all the views end up with the ImageView. I am suspecting that its because of the ImageView which I am adding dynamically.

How do I implement this ?

Thanks in advance.

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/67344.html

上一篇: Firefox会话cookie

下一篇: 在ListViewAdapter中的onClickListener方法内使用holder变量