更改按钮背景时出现SimpleAdapter问题,它会更改所有项目

时间:2013-03-30 01:16:04

标签: android simpleadapter

当我填充SimpleAdapter时。在getView()方法中,我检查视频是否已存在。如果它已经存在,我会将按钮的图像更改为“播放”图像。否则,我将保留其“下载”图像。问题是当我滚动列表时,它将所有按钮更改为“播放”图像。这是我的代码,我做错了什么?

public View getView(int position, View convertView, ViewGroup parent) {
    View row=super.getView(position, convertView, parent);

    TextView videoIdText = (TextView) row.findViewById(R.id.videoId);
    Button downloadButton = (Button) row.findViewById(R.id.videoDownload);

    final String videoId = videoIdText.getText().toString();

    if (videoExists(videoId)) {

        downloadButton.setBackgroundResource( R.drawable.ic_play );
        Drawable d =  downloadButton.getBackground();
        d.setColorFilter(Color.parseColor("#00AA00"),Mode.SRC_ATOP);

        downloadButton.setOnClickListener(new OnClickListener(){ 
            @Override
            public void onClick(View view) {
                if (activity !=null){
                    ((FeedActivity)activity).playVideo(getVideoPath(videoId));
                }       
            }
        });     
    }else{
        downloadButton.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View view) {
                DownloadTask task = new DownloadTask();
                task.setVideoId(videoId);
                task.execute();
            }
        });         
    }

2 个答案:

答案 0 :(得分:2)

在“else”的if(videoExists(videoId))子句中,您需要设置默认的“下载”按钮图像和彩色滤镜。

这是因为当您滚动列表时会重复使用这些项目,因此具有新设置的按钮将重复使用那些当前未播放的其他项目的新设置。


示例

if (videoExists(videoId)) {
    downloadButton.setBackgroundResource( R.drawable.ic_play );
    Drawable d =  downloadButton.getBackground();
    d.setColorFilter(Color.parseColor("#00AA00"), Mode.SRC_ATOP);
    ...
} else {
    downloadButton.setBackgroundResource( R.drawable.ic_download );
    Drawable d =  downloadButton.getBackground();
    d.setColorFilter(Color.parseColor("<download-color>"), Mode.SRC_ATOP);
    ...
}

答案 1 :(得分:0)

@David Manpearl提到,我需要设置原始图片,但我也需要将按钮存储在标签中

public View getView(int position, View convertView, ViewGroup parent) {
    View row=super.getView(position, convertView, parent);

    TextView videoIdText = (TextView) row.findViewById(R.id.videoId);
    Button downloadButton = (Button) row.findViewById(R.id.videoDownload);

    final String videoId = videoIdText.getText().toString();

    if ( row.getTag() == null){
         row.setTag(downloadButton);
    }else{
        downloadButton = (Button) row.getTag();
    }

    if (videoExists(videoId)) {

        downloadButton.setBackgroundResource( R.drawable.ic_play );
        Drawable d =  downloadButton.getBackground();
        d.setColorFilter(Color.parseColor("#00AA00"),Mode.SRC_ATOP);

        downloadButton.setOnClickListener(new OnClickListener(){ 
            @Override
            public void onClick(View view) {
                if (activity !=null){
                    ((FeedActivity)activity).playVideo(getVideoPath(videoId));
                }       
            }
        });     
    }else{
        downloadButton.setBackgroundResource( R.drawable.ic_download );
        downloadButton.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View view) {
                DownloadTask task = new DownloadTask();
                task.setVideoId(videoId);
                task.execute();
            }
        });         
    }