0

i have problem my json element has json array i looped the array from adapter so when i scroll down and scroll up again the elements appended from json array is duplicated from 2 to 4 then 8 etc how to fix this , this is my code below

the problem in this code

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

full code

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 Answer 1

1

In a ListView its not guaranteed that the same View is showing the same JSONObject after scrolling - you should therefore probably remove all subviews of your

holder.attachment_bit_handler

before adding new ones.

Sign up to request clarification or add additional context in comments.

3 Comments

holder.attachment_bit_handler.removeAllViews(); done and fixed thanks ;)
where this code be placed ? in the getView method ?
Yup - you can see it in the lower code block of the question. You should also notice setting some holder as the View's tag, which is a best practice to retrieve the list elements subviews performantly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.