0

我的列表视图中有重复的操作问题我在互联网上进行了很多研究都失败了我的问题是当我单击第 1 项时它已展开并且当我向下滚动分页时附加更多项目并且我发现第 10 号项目也已展开,另一个例子,如果我切换项目编号 2,则编号 2 和编号 12 的项目将同时切换!

如何解决这个重复的问题?

这是列表视图类

public class Ringtones_Listview extends ArrayAdapter<Object> {

    int resource;
    String response;
    Context context;
    private LayoutInflater mInflater;
    MediaPlayer mp = null;
    ImageView play_clicked;
    View vv = null;
    String lang = Locale.getDefault().getDisplayLanguage();
    ArrayList ob;
    Integer clickedposition; 

    public Ringtones_Listview(Context context, int resource, ArrayList objects) {
        super(context, resource,objects);

        this.context = context;
        this.ob = objects;
        this.resource = resource;
        mInflater = LayoutInflater.from(context);
    }
    public int getCount() {
        return ob.size();
    }

    public JSONObject getItem(JSONObject position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }
    static class ViewHolder {

        ImageView play ;
        ImageView download ;
        TextView  rtitle;
        TextView  size;
        TextView  downloads;
        TextView  personname;
        TextView  date;
        RatingBar ratingsmall;
        ImageView ratebutton;
        long    tonid;
        TextView  voters;
        LinearLayout more;
    }

    @SuppressWarnings("deprecation")
    public View getView(final int position, View convertView, ViewGroup parent)
    {
        ViewHolder holder;
        //Get the current location object
        JSONObject r = (JSONObject) getItem(position);

        //Inflate the view
       if(convertView == null)
       {

            convertView = mInflater.inflate(R.layout.ringtone_bit, null);
            holder = new ViewHolder();
            convertView.setTag(holder);


            holder.play        = ( ImageView ) convertView.findViewById(R.id.play);
            holder.download    = ( ImageView ) convertView.findViewById(R.id.download);
            holder.ratebutton  = ( ImageView ) convertView.findViewById(R.id.ratebutton);
            holder.rtitle      = (TextView)  convertView.findViewById(R.id.rtitle);
            holder.size        = (TextView)  convertView.findViewById(R.id.size);
            holder.downloads   = (TextView)  convertView.findViewById(R.id.downloads);
            holder.voters      = (TextView)  convertView.findViewById(R.id.voters);
            holder.personname  = (TextView)  convertView.findViewById(R.id.personname);
            holder.date        = (TextView)  convertView.findViewById(R.id.date);
            holder.ratingsmall = (RatingBar) convertView.findViewById(R.id.ratingsmall);
            holder.more = ( LinearLayout ) convertView.findViewById(R.id.more);

      }
      else
       {
            holder = (ViewHolder) convertView.getTag();
       }

           //hide More information
           holder.more.setVisibility(View.GONE); 

          try {
            Date date_g = new Date(r.getLong("timestamp") * 1000);
            holder.date.setText(date_g.toLocaleString());
            } catch (JSONException e2) {}

           //set person name
            try {
                String client_name = ( r.getString("personname").equals( "null" ) == true ) ? "ghost" : r.getString("personname");
                holder.personname.setText(client_name);
            } catch (JSONException e2) {}

            //set total votars and vote avarage
            try {
                float z = (float) r.getInt("rate");
                holder.voters.setText(" ( "+ r.getLong("voters") +" ) / " + z);
            } catch (JSONException e2) {}            
            //set rating bar
            try {
                float z = (float) r.getInt("rate");
                holder.ratingsmall.setRating(z);
            } catch (JSONException e2) {}           
            //set ringtone Name as defualt device language
            try {
                String name = ( lang.equals( "English" ) == true ) ?  r.getString("en_name") : r.getString("ar_name");
                holder.rtitle.setText(name);
            } catch (JSONException e2) {}

            //ringtone file size
            try {
                holder.size.setText(r.getString("size"));
            } catch (JSONException e2) {}

            //set downloads
            try {
                holder.downloads.setText(String.valueOf( r.getLong("downloads") ));
            } catch (JSONException e2) {}

            //set ringtone ID toneid
            try {
                  holder.tonid = r.getLong("toneid");
                  holder.download.setTag(r.getLong("toneid"));
                  holder.ratebutton.setTag(r.getLong("toneid"));
                  holder.play.setId((int) r.getLong("toneid"));
                  convertView.setId((int) r.getLong("toneid"));

               } catch (JSONException e1) {}

            //set download stram url to play icon
            try {
                holder.play.setTag(r.getString("stream_url"));
            } catch (JSONException e) {}

            //add play listener test Ringtone before download it
            holder.play.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(final View v) {

                    clickedposition=position;
                    boolean playit = false;

                     v.setMinimumHeight(200);

                     //stop Media player dont play any thing
                     if ( mp != null )
                     {
                         mp.stop();
                         mp.release();
                         mp = null;
                     }

                     //check for last clicked item
                     if ( vv != null )
                     {
                         //set last played item to play icon
                         vv.setBackgroundResource(R.drawable.play); 

                         //check if last played is not smae clicked item
                         if( v.getTag().toString().equals( vv.getTag().toString() ) == false )
                         {
                             playit = true; 
                         }else
                        //its same item dont play stop here  
                         {
                             vv     = null;
                         }
                     }else
                    //nothing played yet play it
                     {
                         playit = true; 
                     }


                     if ( playit )
                     {

                         vv = v;

                         v.setBackgroundResource(R.drawable.preparing_player); 


                         AnimationDrawable frameAnimation = (AnimationDrawable) v.getBackground();
                         frameAnimation.start();


                         String stramUrl = String.valueOf(v.getTag());


                         try {

                             mp = new MediaPlayer();
                             mp.setAudioStreamType(AudioManager.STREAM_MUSIC);   
                             mp.setDataSource(stramUrl);
                             mp.prepareAsync();


                         } catch (Exception e) {}


                         //preparing straming
                         mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {

                            @Override
                            public void onPrepared(MediaPlayer mps) {

                                 v.setBackgroundResource(R.drawable.played); 
                                 mps.start(); 
                            }
                        });

                        mp.setOnCompletionListener( new MediaPlayer.OnCompletionListener() {

                            @Override
                            public void onCompletion(MediaPlayer mp) {
                                 v.setBackgroundResource(R.drawable.play); 
                            }
                        });

                     }          




                }
            });




        return convertView;
    }

}
4

1 回答 1

1

ListView出于性能原因将重新使用视图,它每次都会使用您的第一个膨胀视图,因此当您展开它并向下滚动时,其他视图也使用相同的布局,您将能够看到展开的视图。

编辑:

vv.setBackgroundResource(R.drawable.play); // 不点击播放按钮时的默认图片getView()

我认为一次只会播放一首歌曲,因此请使用 int 来跟踪已点击的 Imageview

像这样

整数点击位置;把它当作一个领域,然后

在您getView()单击playimageview 时,将其位置设置为 clickedposition。

clickedposition=position;

在getview中像这样检查

if(clickedposition!=null)
{
 v.setBackgroundResource(R.drawable.played); 
}

编辑

int检查位​​置=-1;

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

           holder.play.setBackgroundResource(R.drawable.play);
           if(checkposition!=-1)
          {
           holder.play.setBackgroundResource(R.drawable.played); 
           }


        return convertView;
    }
于 2013-04-26T16:13:43.207 回答