2013-07-29 2 views
0

내 json 요소에 json 배열이 있습니다. 어댑터에서 배열을 반복 할 때 스크롤을 아래로 내리면 json 배열에 추가 된 요소가 2에서 4, 8 등으로 복제됩니다.이 코드는 어떻게 수정합니까? 이 때문에 prob는해야한다 -android listview JSONArray 요소가 중복 되었습니까?

같은보기 스크롤 후 동일한 된 JSONObject를 보여주는 것이 보장되지는 ListView를이 코드

//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

의 문제 아래에 새 하위보기를 추가하기 전에

holder.attachment_bit_handler 

의 하위보기를 모두 제거하십시오.

+0

'holder.attachment_bit_handler.removeAllViews(); 완료 및 고마워;) –

+0

이 코드는 어디에 배치해야합니까? getView 메소드에서? – Udo

+0

넵 - 질문의 하위 코드 블록에서 확인할 수 있습니다. 또한 일부 보유자를 View 태그로 설정하는 것이 중요합니다. 이는 목록 요소 하위 뷰를 효율적으로 검색하는 것이 가장 좋습니다. –