title: element overwriting while using list.add(0,arrayObj) to add last item at top in arraylist android
body: <p>i am showing some server data using recyclerview which will show updated data to the top,and to update list i am calling a method after 15 sec each, first time i req server its working fine but after that when i am updating list with server data 15 sec each and trying to add the new item to top and show, its not working well ,more specific the problem is while the last item is pic and updated data is text its merged in output.</p>
  
  <p><a href="https://i.stack.imgur.com/NLk35.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/NLk35.png" alt="enter image description here"></a></p>
  
  <p><a href="https://i.stack.imgur.com/vbEhe.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/vbEhe.jpg" alt="enter image description here"></a></p>
  
  <p>see the pics. last item was webadeal pic and when i updated list with data <strong>"test"</strong> its merged in UI.</p>
  
  <p>here is my code:</p>
  
  <p>fragment:</p>
  
  <pre><code>oncreateview(){
       //adapter initialization
          linearLayoutManager=new LinearLayoutManager(context_newsfeed);
  
          recyclerView.setLayoutManager(linearLayoutManager);
  
              newsFeedAdapter = new NewsFeedAdapter(newsFeedClassArrayList, context_newsfeed);
  
              recyclerView.setAdapter(newsFeedAdapter);
              DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(context_newsfeed, linearLayoutManager.getOrientation());
              recyclerView.addItemDecoration(dividerItemDecoration);
              if (WebViewActivity.getScrollPosition() != null) {
                  recyclerView.scrollToPosition(Integer.valueOf(WebViewActivity.getScrollPosition()));
              }
              }
  //below are two method. first is called for once which working well but second is for 15 sec each.
  
  void updateNewsFeed(){
  
  //        newsFeedClassArrayList=new ArrayList&lt;&gt;();
  
          postId_ArrayList=new ArrayList&lt;&gt;();
  
  //   allArrayList=new ArrayList&lt;&gt;();
  
          try {
              newsFeedQuery= URLEncoder.encode("userId","UTF-8")+"="+URLEncoder.encode(ServiceClass.getDefaults("USERID",context_newsfeed),"UTF-8")
                      + "&amp;" + URLEncoder.encode("updatePostId","UTF-8")+"="+ URLEncoder.encode(lastPostId,"UTF-8");
  
              if(asyncTaskClass==null || asyncTaskClass.getStatus()== AsyncTask.Status.FINISHED) {
  
                  asyncTaskClass = new AsyncTaskClass(context_newsfeed, new AsyncTaskResult() {
                      @Override
                      public void AsyncTaskResult(String result) throws JSONException {
  
                          Log.v("ResDUpdate", result);
                          swipeRefreshLayout.setRefreshing(false);
  
                              JSONObject jsonObject = new JSONObject(result);
                              JSONArray jsonArray = jsonObject.getJSONArray("result");
                          if (jsonArray.length() &gt; 0){
                          for (int i = 0; i &lt; jsonArray.length(); i++) {
                              JSONObject tmpjsonObject = new JSONObject(jsonArray.optString(i));
                              newsFeedClass_updated = new NewsFeedClass();
  
                              if (tmpjsonObject.getString("funpage").equals("1")) {
  
                                  newsFeedClass_updated.setUserId_friends(tmpjsonObject.getString("user_id"));
                                  newsFeedClass_updated.setPostId(tmpjsonObject.getString("id"));
  
                              /*if(postId==(null) || postId.equals("")|| Integer.valueOf(postId)&lt;Integer.valueOf(tmpjsonObject.getString("id")))
                              {
                                  ServiceClass.setDefaults("postId",tmpjsonObject.getString("id"),context_newsfeed);
                              }*/
                                  // make post id arrayList
                                  ContactListClass contactListClass_postId = new ContactListClass();
                                  contactListClass_postId.setContactName(tmpjsonObject.getString("id"));   //post id
                                  contactListClass_postId.setContactNumber(String.valueOf(i));  //index no
                                  postId_ArrayList.add(contactListClass_postId);
  
                                  //updated post id
                                  if (Integer.valueOf(tmpjsonObject.getString("id")) &gt; Integer.valueOf(lastPostId)) {
                                      lastPostId = tmpjsonObject.getString("id");
                                  }
  
                                  newsFeedClass_updated.setUserPicUrl_newsFeed(tmpjsonObject.getString("profile_pic"));
                                  newsFeedClass_updated.setUserProName(tmpjsonObject.getString("author_name"));
                                  newsFeedClass_updated.setStatus(tmpjsonObject.getString("post_content"));
                                  newsFeedClass_updated.setLikeCount(tmpjsonObject.getString("post_likes"));
                                  newsFeedClass_updated.setWowCount(tmpjsonObject.getString("post_wows"));
                                  newsFeedClass_updated.setBlehCount(tmpjsonObject.getString("post_blehs"));
                                  newsFeedClass_updated.setDislikeCount(tmpjsonObject.getString("post_dislikes"));
                                  newsFeedClass_updated.setTimeStamp(tmpjsonObject.getString("post_time"));
                                  newsFeedClass_updated.setVideo("");
                                  newsFeedClass_updated.setYoutubeVideo("");
                                  newsFeedClass_updated.setIsSharedValue(tmpjsonObject.getString("is_shared"));
                                  newsFeedClass_updated.setIsFriend(tmpjsonObject.getString("is_by_friend"));
  
  
  
                                  //post attachment // here image if more than one
                                  imageList_FunPost = new ArrayList&lt;&gt;();
                                  if (tmpjsonObject.getJSONArray("attachments").length() &gt; 0) {
  
                                      JSONArray jsonArray_attachment = tmpjsonObject.getJSONArray("attachments");
                                      for (int i_attachment = 0; i_attachment &lt; jsonArray_attachment.length(); i_attachment++) {
                                          JSONObject jsonObject_attachment = new JSONObject(jsonArray_attachment.optString(i_attachment));
                                          ContactListClass contactListClass = new ContactListClass();
                                          contactListClass.setContactName(InfoSetClass.getBaseUrl() + jsonObject_attachment.getString("file_path"));// post picture url save
                                          contactListClass.setContactNumber((tmpjsonObject.getString("id")));
                                          imageList_FunPost.add(contactListClass);
                                      }
                                      newsFeedClass_updated.setImageList(imageList_FunPost);
                                  }
                                  Log.v("ResDfsd","tes "+ newsFeedClassArrayList);
                                  newsFeedClassArrayList.add(0,newsFeedClass_updated);// but as i need to show last item at top thats why i do this and prb raise.
                                  Log.e("newsFeedClassArrayList","after adding new data from api response "+ newsFeedClassArrayList);
  
                              }
                          }
                              //that is fine, may be problem is else where
  //                        allArrayList.addAll(newsFeedClassArrayList);
  //                            newsFeedAdapter.swapList(newsFeedClassArrayList);
   //                       newsFeedAdapter.notifyItemInserted(0);
                           newsFeedAdapter.notifyDataSetChanged();
                          //set list to service class
                          ServiceClass.setPostId_list(postId_ArrayList);
                      }
                          Log.v("ResDfsd","tesdf "+ newsFeedClassArrayList);
                      }
                  });
                  asyncTaskClass.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, "POST", "mobileApi/getUpdatePost", newsFeedQuery);
              }
          } catch (UnsupportedEncodingException e) {
              e.printStackTrace();
          }
      }
  </code></pre>
  
  <p>adapter:</p>
  
  <pre><code>@Override
      public void onBindViewHolder(final MyViewHolder holder, final int position) {
  
          holder.rightArrowViewPagerId.setVisibility(View.GONE);
          holder.leftArrowViewPagerId.setVisibility(View.GONE);
          currentPageNo=holder.viewPager.getCurrentItem();
  //        mediaPlayer=new MediaPlayer();
          newsFeedClass=newsFeedClassArrayList.get(position);
          Picasso.with(context).load(InfoSetClass.getBaseUrl()+newsFeedClass.getUserPicUrl_newsFeed()).centerCrop().transform(new CircleTransform()).resize(50,50).into(holder.userProPicImg);
  
          holder.userProNameTvId.setText(newsFeedClass.getUserProName());
  
          if(newsFeedClass.getLikeCount()!=null)
              totalLikeWow=Integer.valueOf(newsFeedClass.getLikeCount())+Integer.valueOf(newsFeedClass.getBlehCount()) +Integer.valueOf(newsFeedClass.getDislikeCount())+Integer.valueOf(newsFeedClass.getWowCount());
          holder.likeCountTv.setText(String.valueOf(totalLikeWow));
  
          if(newsFeedClass.getCommentCount()!=null)
          holder.commentCountTv.setText(String.valueOf(newsFeedClass.getCommentCount()));
  
          if(newsFeedClass.getShareCount()!=null){
              holder.shareCountTv.setText(String.valueOf(newsFeedClass.getShareCount()));
          }
  
          if(!((newsFeedClass.getStatus().toLowerCase().contains(".jpg"))||(newsFeedClass.getStatus().toLowerCase().contains(".png"))||
                  (newsFeedClass.getStatus().toLowerCase().contains("https"))||(newsFeedClass.getStatus().toLowerCase().contains("http")))) {
              holder.statusTv.setText(Html.fromHtml(newsFeedClass.getStatus()));
  //            holder.viewPager.setVisibility(View.GONE);
          }
          else {
  
              holder.newsFeedImageView.loadData(newsFeedClass.getStatus(), "text/html", null);
              holder.newsFeedImageView.getSettings().setJavaScriptEnabled(true);
              WebSettings webSettings = holder.newsFeedImageView.getSettings();
              webSettings.setGeolocationEnabled(true);
              webSettings.getJavaScriptEnabled();
              webSettings.getMediaPlaybackRequiresUserGesture();
  //          holder.viewPager.setVisibility(View.GONE);
  
          }
  
          //image slider
          if(newsFeedClass.getImageList()!=null){
  
              holder.viewPager.setVisibility(View.VISIBLE);
              viewPagerAdapter=new ViewPagerAdapter(context,newsFeedClass.getImageList());
              holder.viewPager.setAdapter(viewPagerAdapter);
  
              if(viewPagerAdapter.getCount()&gt;1){
                  holder.rightArrowViewPagerId.setVisibility(View.VISIBLE);
              }
              if(holder.viewPager.getCurrentItem()&gt;1){
                  holder.leftArrowViewPagerId.setVisibility(View.VISIBLE);
              }
              if(holder.viewPager.getCurrentItem()==0){
                  holder.leftArrowViewPagerId.setVisibility(View.GONE);
              }
              if(holder.viewPager.getCurrentItem()==viewPagerAdapter.getCount()){
                  holder.rightArrowViewPagerId.setVisibility(View.GONE);
              }
              holder.rightArrowViewPagerId.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View view) {
                      if(holder.viewPager.getCurrentItem()&lt;viewPagerAdapter.getCount()){
                          if(holder.viewPager.getCurrentItem()&lt;=viewPagerAdapter.getCount()){
                              holder.viewPager.setCurrentItem(currentPageNo++,true);
                          }
                      }
                  }
              });
  //            holder.viewPagerArrowIndicator.bind(holder.viewPager);
  //            holder.viewPagerArrowIndicator.setArrowIndicatorRes(R.drawable.arrow_left_black_24dp,R.drawable.arrow_right_black_24dp);
              viewPagerAdapter.notifyDataSetChanged();
  
          }else {
  //            holder.viewPager.setVisibility(View.GONE);
              relativeLayout.setVisibility(View.GONE);
          }
  
          holder.timeStampTvId.setText(newsFeedClass.getTimeStamp());
  
  
      }
  </code></pre>