Android ListView与自定义Arrayadapter图像重复出现

时间:2012-02-01 12:37:17

标签: android arrays listview

我正试图在点击列表视图项目时设置一个播放图标,就像在音乐播放器中一样。

我正在使用自定义阵列适配器,一切正常,但是当我向上/向下滚动并选择列表中的任何项目时,之前的播放图标会消失。

但是,如果我不滚动,只需点击该项目就可以正常工作。

OnclickListener:

    listChannels.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            // TODO Auto-generated method stub


            if (oldView != null) {
                oldView.setDrawingCacheEnabled(true);
                ImageView icon = (ImageView) oldView
                        .findViewById(R.id.icon);
                icon.setImageBitmap(null);
                icon.setVisibility(ImageView.GONE);
                icon.invalidate();
            }

            oldView = arg1;
            selectedItem = arg2;
            ImageView icon = (ImageView) arg1.findViewById(R.id.icon);
            icon.setImageResource(R.drawable.play);
            icon.setVisibility(ImageView.VISIBLE);

        }
    });

阵列适配器:

private class ListArrayAdapter extends ArrayAdapter<String> {
    public ListArrayAdapter(Context context, int textViewResourceId,
            String[] arrTitle) {
        super(context, textViewResourceId, arrTitle);

        imageLoader = new ImageLoader(activity.getApplicationContext());
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = getLayoutInflater();
        View row = inflater.inflate(R.layout.listitem_row, parent, false);
        TextView label = (TextView) row.findViewById(R.id.station);


        ImageView imageView = (ImageView) row
                .findViewById(R.id.imageViewDisplay);


            String areaStr = feeds.get(position).getStrLocation();

            imageLoader.DisplayImage(feeds.get(position).getStrImage()
                    .toString(), activity, imageView);

            if (!areaStr.equals("")) {
                TextView area = (TextView) row.findViewById(R.id.area);
                area.setText(areaStr);
                area.setVisibility(TextView.VISIBLE);
            }
            if (selectedItem == position) {
                ImageView icon = (ImageView) row.findViewById(R.id.icon);
                icon.setImageResource(R.drawable.play);
                icon.setVisibility(ImageView.VISIBLE);

            }

        return row;
    }
}

1 个答案:

答案 0 :(得分:1)

您应该拥有这样的自定义适配器类:

public class ListArrayAdapter extends ArrayAdapter<String>{

    Context context;
    ArrayList<String> array=new ArrayList<String>();
    LayoutInflater mInflater;
    int selected;

    public ListArrayAdapter(Context context,int textViewResourceId,ArrayList<String> list)
    {
        super(context, textViewResourceId, list);

        this.context=context;
        array=list; 
        mInflater = LayoutInflater.from(context);
            selected=list.size()+1;     
    }   
    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {       
        final ViewHolder holder;

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

                 holder.label = (TextView) convertView.findViewById(R.id.station);
                 holder.imageView = (ImageView) convertView.findViewById(R.id.imageViewDisplay);                     
                 holder.icon = (ImageView) convertView.findViewById(R.id.icon);
                 holder.area = (TextView) row.findViewById(R.id.area);

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

       // your code goes here 

       convertView.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {

                  selected=position;
                                  holder.icon.setImageResource(R.drawable.play);
                                  holder.icon.setVisibility(View.VISIBLE);
                }
       });
       if(selected==position)
       {
             holder.icon.setImageResource(R.drawable.play);
             holder.icon.setVisibility(View.VISIBLE);
       }
       else 
             holder.icon.setVisibility(View.GONE);

        return convertView;
    }

    static class ViewHolder
    {
       TextView label;
       ImageView imageView;
       ImageView icon;
       TextView area;
    }
}

并且您无需担心listview.onItemClick()中的此问题。