从视图中获取错误的位置

时间:2017-02-01 09:14:43

标签: android listview android-mediaplayer baseadapter

我有一个列表视图,其中我的歌曲来自服务列表中的行有按钮,即(播放/暂停)按钮。我在完成歌曲时遇到问题,因为如果我的歌曲结束,那么我想点击下一行播放按钮,但它会执行点击第4行按钮。但是如果我增加列表行的高度而不是第二个列表行的高度。我也试图使用方法setTag()和getTag(),但没有运气。

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

3 个答案:

答案 0 :(得分:1)

根据您的代码,您应该如下所示,

 @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();

            }
    });

<强>更新

创建名为position_tag的静态字段,然后在按钮单击时分配第一个位置,当媒体播放器完成时,可以在position_tag中添加+1,并检查列表项上是否仍存在该位置。然后,将其添加到媒体播放器。将URL设置为MP,然后在mediaplayer completed listener上使用相同的引用再次启动mediaplayer。

注意:您正在适配器类中创建MediaPlayer对象,当适配器类销毁并重新创建时,重新创建的mediaplayer对象将无法在现有播放媒体文件上运行。

答案 1 :(得分:1)

请将一行更改为getView,它应该可以解决您的问题。

 final Planet planet = (Planet)this.getItem(position);

因为当您从可见列表项大小中单击ListView项目的给定位置时,而不是按有序方式从整个大小中单击。

还将下面的行放入您用来设置标签的“if”子句中。因为每次更新标签时都会更改,因为所有工资新位置都不是实际位置,这是第一次设置列表视图时的膨胀。< / p>

    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);
        viewHolder.playbutton.setTag(position);     //position is a int variable    
    } else {    
        viewHolder = (ViewHolder) convertView.getTag();
    }

答案 2 :(得分:0)

在viewHolder.playbutton.setOnClickListener中,您可以使用viewholder的getAdapterPosition()方法获取所单击项目的位置。在onClickListener中,您可以保存该位置,在onCompletionListener中只需将此位置增加1并播放下一行中的歌曲。