title: Android listview sequence wrong
body: <p>I have listview with multiple items. each row containing image and text in zig zag manner i check position if((position % 2) == 0) then i attach image to left and text to right and in else i set image to right and text to left. it works proper but when i scroll up or scroll down then i getting a problem with sequence. i need listview like this<a href="https://i.stack.imgur.com/4T3oZ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/4T3oZ.png" alt="enter image description here"></a></p>
  
  <p>but when i scroll to listview then i getting like this</p>
  
  <p><a href="https://i.stack.imgur.com/fPXVp.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/fPXVp.png" alt="enter image description here"></a></p>
  
  <pre><code>    public View getView(final int position, View view, ViewGroup parent) {
      final ViewHolder holder;
      if (view == null || view.getTag() == null) {
          holder = new ViewHolder();
  
          view = inflater.inflate(R.layout.adapter_hotel_list_left_layout, null);
  
          // get total display height and width
          WindowManager windowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
          Display display = windowManager.getDefaultDisplay();
          Point size = new Point();
          display.getSize(size);
          int width = size.x;
          int height = size.y;
  
          // divide total width by 2 and store it into newWidth variable
          int newWidth = width / 2;
          Log.d("Width-NewWidth", "" + width + "-" + newWidth);
  
          int marginLeftSide = 0, marginRightTopBottomSide = 0;
  
          // if Utility.deviceTypeFlag == "Tab" then user device is tab then set margins according to tab
          if (Utility.deviceTypeFlag.equalsIgnoreCase("Tab")) {
              float density = mContext.getResources().getDisplayMetrics().density;
              int marginLeft = 15;
              marginLeftSide = (int) (marginLeft * density);
              int marginRightTopBottom = 8;
              marginRightTopBottomSide = (int) (marginRightTopBottom * density);
              newWidth = newWidth - (marginRightTopBottomSide * 3);
  
          } else {
              // set margins accroding to smartphone.
              float density = mContext.getResources().getDisplayMetrics().density;
              int marginLeft = 10;
              marginLeftSide = (int) (marginLeft * density);
              int marginRightTopBottom = 5;
              marginRightTopBottomSide = (int) (marginRightTopBottom * density);
              newWidth = newWidth - (marginRightTopBottomSide * 3);
          }
  
          // if Utility.deviceTypeFlag == "Tab" then user device is tab then set margins according to tab
          if (Utility.deviceTypeFlag.equalsIgnoreCase("Tab")) {
              int direction = Integer.parseInt(arrayListHotelDetails.get(position).get("directionFlag"));
              if ((position % 2) == 0) {
                  Log.d("position", String.valueOf(position));
  
                  // check position if it is even then set hotel image to left and hotel name to right.
                  holder.relativeLayout_Image = (RelativeLayout) view.findViewById(R.id.relativelayout_hll);
                  holder.imageviewHotel = (ImageView) view.findViewById(R.id.imageview_Hotel_Name_ahl);
                  RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(newWidth, Utility.relativelayout_Height);
                  layoutParams.setMargins(marginLeftSide, marginLeftSide, marginRightTopBottomSide, 0);
                  holder.relativeLayout_Image.setLayoutParams(layoutParams);
  
                  holder.relativeLayout_Text = (RelativeLayout) view.findViewById(R.id.relativelayout2_hll);
                  RelativeLayout.LayoutParams layoutParams1 = new RelativeLayout.LayoutParams(newWidth, Utility.relativelayout_Height);
                  layoutParams1.setMargins(marginRightTopBottomSide, marginLeftSide, marginLeftSide, 0);
                  layoutParams1.addRule(RelativeLayout.RIGHT_OF, R.id.relativelayout_hll);
                  holder.relativeLayout_Text.setLayoutParams(layoutParams1);
  
                  holder.textViewHotelName = (TextView) view.findViewById(R.id.textview_Hotel_Name);
              } else {
                  // else set hotel image to right and hotel name textview to left.
                  holder.relativeLayout_Image = (RelativeLayout) view.findViewById(R.id.relativelayout_hll);
                  holder.imageviewHotel = (ImageView) view.findViewById(R.id.imageview_Hotel_Name_ahl);
                  RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(newWidth, Utility.relativelayout_Height);
                  layoutParams.setMargins(marginRightTopBottomSide, marginLeftSide, marginLeftSide, 0);
                  layoutParams.addRule(RelativeLayout.RIGHT_OF, R.id.relativelayout2_hll);
                  holder.relativeLayout_Image.setLayoutParams(layoutParams);
  
                  holder.relativeLayout_Text = (RelativeLayout) view.findViewById(R.id.relativelayout2_hll);
                  RelativeLayout.LayoutParams layoutParams1 = new RelativeLayout.LayoutParams(newWidth, Utility.relativelayout_Height);
                  layoutParams1.setMargins(marginLeftSide, marginLeftSide, marginRightTopBottomSide, 0);
                  holder.relativeLayout_Text.setLayoutParams(layoutParams1);
                  holder.textViewHotelName = (TextView) view.findViewById(R.id.textview_Hotel_Name);
                  holder.textViewHotelName.setGravity(Gravity.RIGHT);
              }
  
          } else {
              int direction = Integer.parseInt(arrayListHotelDetails.get(position).get("directionFlag"));
              // set margins according to smartphone
              if ((position % 2) == 0) {
                  Log.d("position", String.valueOf(position));
                  // check position if it is even then set hotel image to left and hotel name to right.
  
                  holder.relativeLayout_Image = (RelativeLayout) view.findViewById(R.id.relativelayout_hll);
                  holder.imageviewHotel = (ImageView) view.findViewById(R.id.imageview_Hotel_Name_ahl);
                  RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(newWidth, Utility.relativelayout_Height);
                  layoutParams.setMargins(marginLeftSide, marginRightTopBottomSide, marginRightTopBottomSide, marginRightTopBottomSide);
                  holder.relativeLayout_Image.setLayoutParams(layoutParams);
  
                  holder.relativeLayout_Text = (RelativeLayout) view.findViewById(R.id.relativelayout2_hll);
                  RelativeLayout.LayoutParams layoutParams1 = new RelativeLayout.LayoutParams(newWidth, Utility.relativelayout_Height);
                  layoutParams1.setMargins(marginRightTopBottomSide, marginRightTopBottomSide, marginLeftSide, marginRightTopBottomSide);
                  layoutParams1.addRule(RelativeLayout.RIGHT_OF, R.id.relativelayout_hll);
                  holder.relativeLayout_Text.setLayoutParams(layoutParams1);
  
                  holder.textViewHotelName = (TextView) view.findViewById(R.id.textview_Hotel_Name);
              } else {
                  // else set hotel image to right and hotel name textview to left.
                  holder.relativeLayout_Image = (RelativeLayout) view.findViewById(R.id.relativelayout_hll);
                  holder.imageviewHotel = (ImageView) view.findViewById(R.id.imageview_Hotel_Name_ahl);
                  RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(newWidth, Utility.relativelayout_Height);
                  layoutParams.setMargins(marginRightTopBottomSide, marginRightTopBottomSide, marginLeftSide, marginRightTopBottomSide);
                  layoutParams.addRule(RelativeLayout.RIGHT_OF, R.id.relativelayout2_hll);
                  holder.relativeLayout_Image.setLayoutParams(layoutParams);
  
                  holder.relativeLayout_Text = (RelativeLayout) view.findViewById(R.id.relativelayout2_hll);
                  RelativeLayout.LayoutParams layoutParams1 = new RelativeLayout.LayoutParams(newWidth, Utility.relativelayout_Height);
                  layoutParams1.setMargins(marginLeftSide, marginRightTopBottomSide, marginRightTopBottomSide, marginRightTopBottomSide);
                  holder.relativeLayout_Text.setLayoutParams(layoutParams1);
                  holder.textViewHotelName = (TextView) view.findViewById(R.id.textview_Hotel_Name);
                  holder.textViewHotelName.setGravity(Gravity.RIGHT);
  
              }
          }
          view.setTag(holder);
  
      } else {
          holder = (ViewHolder) view.getTag();
      }
      // get hotel name from arraylist set it to textview.
      holder.textViewHotelName.setText(arrayListHotelDetails.get(position).get("hotelName"));
      Log.d("textViewHotelName adapter", arrayListHotelDetails.get(position).get("hotelName"));
      String hotelId = "";
      hotelId = arrayListHotelDetails.get(position).get("hotelId");
      Log.d("hotelId", hotelId);
      // set image using picasso to image view.
      Picasso.with(mContext).load("https://s3-us-west-2.amazonaws.com/foodappdev/hotel/" + hotelId + ".jpg").placeholder(R.drawable.default_hotel).error(R.drawable.default_hotel).into(holder.imageviewHotel);
  
      return view;
  }
  </code></pre>