Search code examples
androidlistviewandroid-mediaplayerbaseadapter

Getting wrong position from view


I have a list view in which I have songs comes from service in a list ever row has button which is (Play/Pause) Button. I am having a problem on On complete of songs because if my Song ends then I want to perform click on Next row play button but it performs click on the 4th row button. but if I increase the height of the list row than it plays the second one. I am also trying to use method setTag() And getTag() but no luck.

 @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        final Planet planet = (Planet) planetList.get(position);


        viewHolder = null;

        // Create a new row view
        if (convertView == null) {
            viewHolder = new ViewHolder();
            LayoutInflater layoutInflater = (LayoutInflater) contexts.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            convertView = layoutInflater.inflate(R.layout.list_rows_relative, parent, false);

            viewHolder.playbutton = (ImageView) convertView.findViewById(R.id.CheckBox01);

            }
            convertView.setTag(viewHolder);

        } else {

            viewHolder = (ViewHolder) convertView.getTag();

        }

    viewHolder.playbutton.setTag(position_tag);     //position_tag is a int variable


viewHolder.playbutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                final int positiont = mainListView.getPositionForView(parentRow);

                    String name = planet.getSong_url();

                    selectedPosition = positiont;
                    position_tag=selectedPosition;
                    position_tag=position_tag+1; // if i use this it gives error on complete button perform click

       position_tag=1; // if i use this then it plays second song perform second button click

        playsong(name);});}



mplayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {

                int hell = position_tag;
                Log.d("position_on",hell+"");  //aFTER 1st SONG COMPLETE these value prints "4" but i need next one  

                    viewHolder.checkBox.findViewById(R.id.CheckBox01).findViewWithTag(position_tag).performClick();
                }
        });

Solution

  • According to your code, you should do like below,

     @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
    
        final Planet planet = (Planet) planetList.get(position);
    
    
        viewHolder = null;
    
        // Create a new row view
        if (convertView == null) {
            viewHolder = new ViewHolder();
            LayoutInflater layoutInflater = (LayoutInflater) contexts.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    
            convertView = layoutInflater.inflate(R.layout.list_rows_relative, parent, false);
    
            viewHolder.playbutton = (ImageView) convertView.findViewById(R.id.CheckBox01);
    convertView.setTag(viewHolder);
    
    
    
        } else {
    
            viewHolder = (ViewHolder) convertView.getTag();
    
        }
    
    viewHolder.playbutton.setTag(position);     //position_tag is a int variable
    
    
     viewHolder.playbutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
    
                final int positiont = Integer.parseInt(v.getTag().toString());
                PlanetList planet=planetList.get(positiont)
                    String name = planet.getSong_url();
    
                    selectedPosition = positiont;
                   position_tag=positiont;
        playsong(name);});}
    
    
    
     mplayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
           position_tag=(position_tag+1)
       final Planet planet = (Planet) planetList.get(position_tag);//UPDATED
       mp.setDataSource(planet.getSongUrl());
       mp.start();
    
                }
        });
    

    UPDATE

    create static field named position_tag then assigned the first position on button click, when the media player is completed, you can add +1 with position_tag, and check that position is still exist on the list item. then, and add it to the media player. set the URL to the MP then again start the mediaplayer with the same reference on mediaplayer completed listener.

    NOTE : you are creating MediaPlayer object in adapter class, when the adapter class destroy and recreated, the recreated mediaplayer object will not work on existing playing media file.