【问题标题】:SimpleAdapter issue when changing the background of a button, it changes for all items更改按钮背景时的 SimpleAdapter 问题,它会更改所有项目
【发布时间】:2013-03-30 01:16:04
【问题描述】:

当我填充我的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();
            }
        });         
    }

【问题讨论】:

    标签: android simpleadapter


    【解决方案1】:

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

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


    示例

    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);
        ...
    }
    

    【讨论】:

      【解决方案2】:

      正如@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();
                  }
              });         
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-04-01
        • 1970-01-01
        相关资源
        最近更新 更多