1

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

}
Nicholas Muir
  • 2,897
  • 8
  • 39
  • 89
  • post your adapter then – pskink Jun 11 '16 at 08:16
  • @pskink edited my question to include my adapter. – Nicholas Muir Jun 11 '16 at 08:26
  • ok so you are calling `item.getSongDuration()` inside `getView` method but `long songDuration` is not used anywhere later on, so what is your problem actually? – pskink Jun 11 '16 at 08:29
  • So the card is a music card with song ect. I have a player that is playing the song on the current card. I want to put a progress bar that is being displayed on the card and relevant only to the song card at the top of the stack. But the getview method is called 4 times for different card meaning I don't know which is the correct duration. – Nicholas Muir Jun 11 '16 at 08:36
  • sorry it is still not clear, make a simple running code like this: http://pastebin.com/E2PHJxMD and explain whats your goal without any additional stuff like picasso, rating bars, cards, click listeners etc – pskink Jun 11 '16 at 09:15
  • Here it is http://pastebin.com/TQeBrfuF – Nicholas Muir Jun 11 '16 at 09:24
  • Sorry looking at your response again i didn't explain well. I am trying to get the duration for the current song from the array in getView. My problem is getView is called multiple times when the cardstack is shown and multiple value of song duration are returned.. – Nicholas Muir Jun 11 '16 at 09:54
  • so see how i updated the `Song` object inside `Runnable`, just follow that way of updating your model data (line 21) – pskink Jun 11 '16 at 09:56

0 Answers0