我正在为android操作系统制作音乐播放器。然而,我面临着这个问题

我正在为android操作系统制作音乐播放器。然而,我面临着这个问题,android,Android,我使用列表视图显示外部卡上的所有歌曲。当用户选择一首特定的歌曲时,该歌曲会高亮显示 这很好,但如果我从与先前突出显示的歌曲相同的可见屏幕中选择另一首歌曲,则两首歌曲将突出显示。如果我再次单击,则会突出显示三首歌曲。如果我滚动屏幕并返回,listview会自动刷新,并突出显示唯一播放的歌曲 请帮忙。 我的适配器的代码 import android.view.View; import android.view.ViewGroup; import android.widget.CursorAdapte

我使用列表视图显示外部卡上的所有歌曲。当用户选择一首特定的歌曲时,该歌曲会高亮显示

这很好,但如果我从与先前突出显示的歌曲相同的可见屏幕中选择另一首歌曲,则两首歌曲将突出显示。如果我再次单击,则会突出显示三首歌曲。如果我滚动屏幕并返回,listview会自动刷新,并突出显示唯一播放的歌曲

请帮忙。 我的适配器的代码

import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.SimpleCursorAdapter ;
import android.widget.TextView;


public class songcursoradapter extends CursorAdapter {
  public songcursoradapter(Context context, Cursor cursor, int flags) {
      super(context, cursor, 0);
  }

  // The newView method is used to inflate a new view and return it, 
  // you don't bind any data to the view at this point. 
  @Override
  public View newView(Context context, Cursor cursor, ViewGroup parent) {
      return LayoutInflater.from(context).inflate(R.layout.songs, parent, false);
  }

  // The bindView method is used to bind all data to a given view
  // such as setting the text on a TextView. 
  @Override
  public void bindView(View view, Context context, Cursor cursor) {
      // Find fields to populate in inflated template
      TextView tvBody = (TextView) view.findViewById(R.id.song_title);
      TextView tvPriority = (TextView) view.findViewById(R.id.artist_titl);
      // Extract properties from cursor
      String body = cursor.getString(cursor.getColumnIndexOrThrow(android.provider.MediaStore.Audio.Media.TITLE));
      String priority = cursor.getString(cursor.getColumnIndexOrThrow(android.provider.MediaStore.Audio.Media.ARTIST));


      // Populate fields with extracted properties
      tvBody.setText(body);
          //if song returned by cursor is the current song playing change color
          if(body==nowplaying.musicSrv.getTitle)                                       //musicSrv plays songs
             tvBody.setTextcolor(MainActivity.color)                                      //color is defined in MainActivity and is static
      tvPriority.setText(priority);
  }


}

您是否在适配器中添加了以下内容:

@Override
public int getItemViewType(int position) {
    return position;
}

@Override
public int getViewTypeCount() {
    return YourArrayList.size();
}
另外,请选择listView模式为singleChoice

xml代码:

android:choiceMode="singleChoice"

如果没有播放,您只需重置颜色,即添加其他部分

if(body==nowplaying.musicSrv.getTitle)    //musicSrv plays songs
   tvBody.setTextcolor(MainActivity.color);
else{
   tvBody.setTextcolor(MainActivity.default_color);
}

请共享您的代码,并在单击listview其他项目时释放以前的音乐播放器线程。似乎您在getView()中出错,请发布适配器代码。它不工作。光标适配器只填充那些可见的视图。如果我选择一首歌,它会改变颜色。但是,如果我选择了另一首歌曲,则上一首选定的歌曲也会保留其颜色。但我希望在我选择一首新歌后,上一首选定的歌曲将失去其颜色。只有当我向上滚动屏幕并将其向下滚动时,上一首选定的歌曲才会更新其颜色,反之亦然。如果我正在使用光标或捕捉器,并且它工作正常,请发送设置适配器的活动代码。