android listview JSONArray元素重复?

时间:2013-07-29 21:39:58

标签: android listview

我有问题我的json元素有json数组我从适配器循环数组所以当我向下滚动并再次向上滚动从json数组追加的元素是从2到4然后8等重复如何解决这个问题,这是我的代码

此代码中的问题

  //if post has attachments
                   if ( attachments.length() != 0 )
                    {
                      holder.post_attachmentbox.setVisibility(View.VISIBLE);

                       //add attachments to post
                       for(int i = 0; i < attachments.length(); i++)
                       {

                          JSONObject attachment = attachments.getJSONObject(i);

                          //prevent add duplicate attachments
                          if( attachment.getLong("postid") == p.getLong("postid"))
                          {
                            Button attach = new Button(context); 
                            attach.setText(attachment.getString("filename"));
                            holder.attachment_bit_handler.addView(attach);
                          }

                       }


                    }else
                    //post not has attachments  
                    {
                        holder.post_attachmentbox.setVisibility(View.GONE);
                    }

完整代码

public class a_ShowThread_pListView extends ArrayAdapter<Object>{


    Context context; 
    private LayoutInflater mInflater; 
    @SuppressWarnings("rawtypes")
    ArrayList ob; 
    int resource ;

     Typeface font_hb ,font_new,font_kufi;

    /*================================================
     *  Setup ListView
     *===============================================*/
    @SuppressWarnings("unchecked")
    public a_ShowThread_pListView (Context context, int resource, @SuppressWarnings("rawtypes") ArrayList objects) {
        super(context, resource,objects);
        this.context  = context;
        this.ob       = objects;
        this.resource = resource;
        mInflater     = LayoutInflater.from(context);

        this.font_hb    =  Typeface.createFromAsset(context.getAssets(),"fonts/hb.ttf");
        this.font_new   =  Typeface.createFromAsset(context.getAssets(),"fonts/neu.ttf");
        this.font_kufi  =  Typeface.createFromAsset(context.getAssets(),"fonts/kufi.ttf");

    }
    /*================================================
     *  Items Counter
     *===============================================*/
    public int getCount() {
        return ob.size();
    }
    /*================================================
     *  Item Posistion JSON
     *===============================================*/
    public JSONObject getItem(JSONObject position) {
        return position;
    }
    /*================================================
     *  Item Position
     *===============================================*/
    public long getItemId(int position) {
        return position;
    }   
    /*================================================
     *  Hold Views inside Chant Bit View
     *===============================================*/
    static class ViewHolder {

        TextView  postername;
        TextView  usertitle;
        TextView  registerdate;
        TextView  posterposts;
        TextView  message;
        HorizontalScrollView post_attachments_scrollview;
        LinearLayout post_attachmentbox;
        LinearLayout attachment_bit_handler;
        TextView  attachment_text;
        JSONArray  attachments;
    }    
    /*================================================
     *  Setup Each View raw by raw
     *===============================================*/
    @SuppressWarnings("unused")
    @SuppressLint("ResourceAsColor") public View getView(final int position, View convertView, ViewGroup parent)
    {
        final ViewHolder holder;
        JSONObject p = (JSONObject) getItem(position);

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

             holder.postername     = (TextView)  convertView.findViewById(R.id.postername); //poster username
             holder.usertitle      = (TextView)  convertView.findViewById(R.id.usertitle);   //poster title
             holder.registerdate   = (TextView)  convertView.findViewById(R.id.registerdate); //poster reigster date
             holder.posterposts    = (TextView)  convertView.findViewById(R.id.posterposts);  // poster posts counter
             holder.message        = (TextView)  convertView.findViewById(R.id.message); //post message
             holder.post_attachments_scrollview  = (HorizontalScrollView)  convertView.findViewById(R.id.post_attachments_scrollview); // attachments view box
             holder.post_attachmentbox = (LinearLayout) convertView.findViewById(R.id.post_attachmentbox); //attachment box hide / show;
             holder.attachment_text = (TextView)  convertView.findViewById(R.id.attachment_text); //Text Attachment legend
             holder.attachment_bit_handler = (LinearLayout) convertView.findViewById(R.id.attachment_bit_handler); //append post attachment to this view
       }
       else
        {
             holder = (ViewHolder) convertView.getTag();
        }


        try {

            //add values
            holder.postername.setText(p.getString("postusername"));
            holder.usertitle.setText(p.getString("usertitle"));
            holder.registerdate.setText(context.getResources().getString(R.string.joindate) +" : "+ p.getString("joindate"));
            holder.posterposts.setText(  context.getResources().getString(R.string.user_posts) +" : " + p.getLong("posts"));
            holder.message.setText( Html.fromHtml(p.getString("pagetext")));

            //fonts
            holder.postername.setTypeface(font_new);
            holder.usertitle.setTypeface(font_new);
            holder.registerdate.setTypeface(font_new);
            holder.posterposts.setTypeface(font_new);
            holder.message.setTypeface(font_kufi);
            holder.attachment_text.setTypeface(font_kufi);


            /********************************
             * if this post is a Thread
             */
            if ( p.getInt("is_thread") == 1 )
            {

                holder.registerdate.setVisibility(View.VISIBLE);
                holder.posterposts.setVisibility(View.VISIBLE);     

            }else
            /********************************
            * Normal Post
            */
            {

                holder.registerdate.setVisibility(View.GONE);
                holder.posterposts.setVisibility(View.GONE);

            }


            /********************************
            * if post has attachments
            */      
            try {

                JSONArray  attachments = p.getJSONArray("attachments");

                    //if post has attachments
                   if ( attachments.length() != 0 )
                    {
                      holder.post_attachmentbox.setVisibility(View.VISIBLE);

                       //add attachments to post
                       for(int i = 0; i < attachments.length(); i++)
                       {

                          JSONObject attachment = attachments.getJSONObject(i);

                          //prevent add duplicate attachments
                          if( attachment.getLong("postid") == p.getLong("postid"))
                          {
                            Button attach = new Button(context); 
                            attach.setText(attachment.getString("filename"));
                            holder.attachment_bit_handler.addView(attach);
                          }

                       }


                    }else
                    //post not has attachments  
                    {
                        holder.post_attachmentbox.setVisibility(View.GONE);
                    }

             } catch (JSONException e) 
             {
                holder.post_attachmentbox.setVisibility(View.GONE);
             }

        } catch (JSONException e) {e.printStackTrace();}


        return convertView;      


    }

}

1 个答案:

答案 0 :(得分:1)

在ListView中,不能保证相同的View在滚动后显示相同的JSONObject - 因此您应该删除

的所有子视图
holder.attachment_bit_handler

在添加新的之前。