我通过阵列适配器使用cardstack。我正在使用getter setter来显示卡上的数据。但我也用getSongDuration()恢复了与该卡相关的歌曲的持续时间。问题是当这个加载时,我在getView方法中执行:
songDuration = item.getSongDuration();
并记录该值,对于不同的位置,它被多次调用。
我需要获取当前歌曲的歌曲持续时间的值。通常我会使用更改的通知数据集,但对于这个实例,我看不出它是如何适用的。
这是我的适配器:
public class SongPreviewCardsDataAdapter extends ArrayAdapter<SongDatabaseMappingAdapter> {
private Context mContext;
public SongPreviewCardsDataAdapter(Context context, int resource) {
super(context, resource);
mContext = context;
}
ImageButton oneStarRating;
ImageButton twoStarRating;
ImageButton threeStarRating;
ImageButton fourStarRating;
ImageButton fiveStarRating;
@Override
public int getCount() {
return super.getCount();
}
@Override
public View getView(int position, final View contentView, ViewGroup parent) {
// Initialise Song Views
final SongDatabaseMappingAdapter item = getItem(position);
long songDuration = item.getSongDuration();
Log.d ("Duration", ""+songDuration);
TextView songName = (TextView) (contentView.findViewById(R.id.songNameTextView));
TextView artist_albumName = (TextView) (contentView.findViewById(R.id.artist_albumView));
com.mikhaellopez.circularimageview.CircularImageView songImage = (CircularImageView) contentView.findViewById(R.id.songImageView);
String ImageURL = (item.getPictureURL());
Picasso
.with(this.getContext())
.load(ImageURL)
.into(songImage);
final CircularProgressBar circularProgressBar = (CircularProgressBar)contentView.findViewById(R.id.songProgresswheel);
circularProgressBar.setColor(ContextCompat.getColor(getContext(), R.color.darkRed));
circularProgressBar.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.darkBlueGrey));
circularProgressBar.setProgressBarWidth(10);
circularProgressBar.setBackgroundProgressBarWidth(10);
com.mikhaellopez.circularimageview.CircularImageView imagelocked = (CircularImageView) contentView.findViewById(R.id.lockedImageView);
// Initialise Rating Buttons
oneStarRating = (ImageButton) contentView.findViewById(R.id.ratingButton1);
twoStarRating = (ImageButton) contentView.findViewById(R.id.ratingButton2);
threeStarRating = (ImageButton) contentView.findViewById(R.id.ratingButton3);
fourStarRating = (ImageButton) contentView.findViewById(R.id.ratingButton4);
fiveStarRating = (ImageButton) contentView.findViewById(R.id.ratingButton5);
if (item.getOneStarRating()) {
oneStarRating.setImageResource(R.drawable.starfullnew);
imagelocked.setVisibility(contentView.GONE);
artist_albumName.setText(item.getArtist() + " - " + item.getAlbumTitle());
songName.setText(item.getSongTitle());
oneStarRating.setClickable(false);
twoStarRating.setClickable(false);
threeStarRating.setClickable(false);
fourStarRating.setClickable(false);
fiveStarRating.setClickable(false);
}
if (item.getTwoStarRating()) {
oneStarRating.setImageResource(R.drawable.starfullnew);
twoStarRating.setImageResource(R.drawable.starfullnew);
imagelocked.setVisibility(contentView.GONE);
artist_albumName.setText(item.getArtist() + " - " + item.getAlbumTitle());
songName.setText(item.getSongTitle());
oneStarRating.setClickable(false);
twoStarRating.setClickable(false);
threeStarRating.setClickable(false);
fourStarRating.setClickable(false);
fiveStarRating.setClickable(false);
}
if (item.getThreeStarRating()) {
oneStarRating.setImageResource(R.drawable.starfullnew);
twoStarRating.setImageResource(R.drawable.starfullnew);
threeStarRating.setImageResource(R.drawable.starfullnew);
imagelocked.setVisibility(contentView.GONE);
artist_albumName.setText(item.getArtist() + " - " + item.getAlbumTitle());
songName.setText(item.getSongTitle());
oneStarRating.setClickable(false);
twoStarRating.setClickable(false);
threeStarRating.setClickable(false);
fourStarRating.setClickable(false);
fiveStarRating.setClickable(false);
}
if (item.getFourStarRating()) {
oneStarRating.setImageResource(R.drawable.starfullnew);
twoStarRating.setImageResource(R.drawable.starfullnew);
threeStarRating.setImageResource(R.drawable.starfullnew);
fourStarRating.setImageResource(R.drawable.starfullnew);
imagelocked.setVisibility(contentView.GONE);
artist_albumName.setText(item.getArtist() + " - " + item.getAlbumTitle());
oneStarRating.setClickable(false);
twoStarRating.setClickable(false);
threeStarRating.setClickable(false);
fourStarRating.setClickable(false);
fiveStarRating.setClickable(false);
}
if (item.getFiveStarRating()) {
oneStarRating.setImageResource(R.drawable.starfullnew);
twoStarRating.setImageResource(R.drawable.starfullnew);
threeStarRating.setImageResource(R.drawable.starfullnew);
fourStarRating.setImageResource(R.drawable.starfullnew);
fiveStarRating.setImageResource(R.drawable.starfullnew);
imagelocked.setVisibility(contentView.GONE);
artist_albumName.setText(item.getArtist() + " - " + item.getAlbumTitle());
songName.setText(item.getSongTitle());
oneStarRating.setEnabled(false);
twoStarRating.setEnabled(false);
threeStarRating.setEnabled(false);
fourStarRating.setEnabled(false);
fiveStarRating.setEnabled(false);
}
oneStarRating.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("Star", "Star Clicked");
item.setOneStarRating(true);
notifyDataSetChanged();
}
});
twoStarRating.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("Star", "Star Clicked");
item.setTwoStarRating(true);
notifyDataSetChanged();
DemoPreviewSongFragment.userHasRatedSong = true;
}
});
threeStarRating.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("Star", "Star Clicked");
item.setThreeStarRating(true);
notifyDataSetChanged();
DemoPreviewSongFragment.userHasRatedSong = true;
}
});
fourStarRating.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("Star", "Star Clicked");
item.setFourStarRating(true);
notifyDataSetChanged();
DemoPreviewSongFragment.userHasRatedSong = true;
}
});
fiveStarRating.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("Star", "Star Clicked");
item.setFiveStarRating(true);
notifyDataSetChanged();
DemoPreviewSongFragment.userHasRatedSong = true;
}
});
return contentView;
}
}