I am using a cardstack through an array adapter. I am using getter setters to display the data on the card. But I am also getting back the duration of a the song that is associated with the card with getSongDuration(). The problem is when this loads and in the getView method i do:
songDuration = item.getSongDuration();
and log the value, it is being called multiple times for different positions.
I need to get the value for the song duration for the current song. Normally I would use the notify dataset changed but for this instance I do not see how that applies.
This is my adapter:
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;
}
}