我正在为Android OS制作音乐播放器。但是我面对这个

时间:2016-08-22 11:37:05

标签: android

我使用列表视图显示外部卡上的所有歌曲。当用户选择特定歌曲时,该歌曲会突出显示。enter image description here

这很好但如果我从与之前突出显示的歌曲相同的可见屏幕中选择另一首歌曲,则会突出显示两首歌曲。如果我再次点击则会突出显示三首歌曲。如果我滚动屏幕然后返回,列表视图会自动刷新并且唯一的播放歌曲会突出显示。enter image description here

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

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


}

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

您是否在适配器中添加了这些:

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

@Override
public int getViewTypeCount() {
    return YourArrayList.size();
}

还要使listView ChoiceMode为singleChoice。

xml代码:

android:choiceMode="singleChoice"