0

我的 Listview 有重复问题,我有分页 listview 一切正常,但问题是当向下滚动并附加新项目时,新项目操作与旧项目重复

例子 :

如果我点击第 11 项,第 11 项和第 1 项都会展开

我在我的视图中搞砸了什么!

将新项目附加到适配器的 AsyncTask

public class ringtones extends AsyncTask<String,String, String> {

       JSONObject data    = null;
       String response    = null;
        Dialog  dialog; 

        @Override
        protected void onPreExecute() {

               dialog = new Dialog(Ringtones.this);
               dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
               dialog.setContentView(R.layout.dialog); 
               dialog.setCancelable(false);
               dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
               dialog.show();

            appendMore = false;
        }
        @Override
        protected String doInBackground(String... params) {


          Log.e("Ring ListView" ,"in Background"); 

          if ( applysort == true )
          {
            page = 1;         
          }

          if (  query.trim().equals("") == false && appendMore == false)
            {
              page = 1;
            }

          response = Api.getringtonesList("&page="+page+"&limit="+limit+"&q="+query+"&sortby="+sortby);

          return response;

        }
        @Override
        protected void onPostExecute(String result) {

            dialog.dismiss();



            if ( response != null )
            {


                if (  query.trim().equals("") == false && appendMore == false)
                {
                    la.clear();
                    la.notifyDataSetChanged();
                }   

                  if ( applysort == true )
                  {
                        la.clear();
                        la.notifyDataSetChanged();  
                        applysort = false;
                  }


             try {
                    data = new JSONObject(result);

                    int last_page = data.getInt("last_page");   

                    try {
                         // Getting Array of Contacts
                         JSONArray  files = data.getJSONArray("ringtones");



                            Toast.makeText(context,  "found " + files.length(), 0).show();

                        if ( files.length() != 0 )
                        {

                          for(int i = 0; i < files.length(); i++)
                           {
                               //extract file info
                               JSONObject f = files.getJSONObject(i);

                               tonebit.add(f);

                            }

                         }  

                        } catch (JSONException e) {
                          e.printStackTrace();
                          Log.e("Ring ListView" ,"Faild get Json ringtones : "+e.getMessage());
                        }

                    if ( last_page > page )
                    {
                        appendMore = true;
                        page++;
                    }

                    la.notifyDataSetChanged();

                  } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    Log.e("Ring ListView" ,"Faild convert String to Json : "+e.getMessage());
                }  



            }


        }
  }

这是 Listview 类

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

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

        this.context = context;

        this.resource = resource;
        mInflater = LayoutInflater.from(context);
    }

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

    public View getView(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);
        }
        else
        {
            holder = (ViewHolder) convertView.getTag();
        }



           //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(onClickListener); 

        convertView.setTag(holder);


        return convertView;
    }

    private OnClickListener onClickListener = new OnClickListener() {
         @Override
         public void onClick(final View v) {

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

             }



         }
    };
}
4

1 回答 1

-1
        try {
            holder.play.setTag(r.getString("stream_url"));
        } catch (JSONException e) {}

        //add play listener test Ringtone before download it
        holder.play.setOnClickListener(onClickListener); 
        //remove this one
        // convertView.setTag(holder);


    return convertView;

删除最后一个 convertView.setTag(holder);在你的 getView();

于 2013-04-26T08:32:13.873 回答