title: GridView Loads the duplicate images and Text (using EndlessScrollListener)
body: <p>I have a custom gridVIew With an ImageView and a TextView in it, and I have set the gridView as a it show 2 coloumns. here is the code of custom_grid_layout.xml</p>
  
  <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/widget44"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:gravity="center_horizontal"
  android:orientation="vertical" &gt;
  
  &lt;ImageView
      android:id="@+id/imgBookCover"
      android:layout_width="88dp"
      android:layout_height="102dp"
      android:adjustViewBounds="true"
      android:background="@drawable/rounded_image_borders"
      android:paddingLeft="5dp"
      android:paddingRight="5dp"
      android:src="@drawable/book5" &gt;
  &lt;/ImageView&gt;
  
  &lt;TextView
      android:id="@+id/txt_BookTitle"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_margin="5dp"
      android:gravity="center_horizontal"
      android:lines="1"
      android:text="TextView"
      android:textColor="@color/White"
      android:textColorHighlight="#656565" &gt;
  &lt;/TextView&gt;
  </code></pre>
  
  <p></p>
  
  <p>and here is the layout of gridview.xml</p>
  
  <pre><code> &lt;GridView
      android:id="@+id/gridview"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:layout_alignParentBottom="true"
      android:layout_below="@+id/Rel_Spinner"
      android:layout_centerHorizontal="true"
      android:gravity="center"
      android:numColumns="auto_fit"
      android:stretchMode="columnWidth" &gt;
  &lt;/GridView&gt;
  </code></pre>
  
  <ul>
  <li>Its loading the images from server and showing the correct images and text for the first time, as I have implemented the EndlessScrollListner class in my project.</li>
  <li>In the first itration im loading 12 images from the server, when i Scroll down the gridView to end it sends the second request to Load 12 more images and text from the server.</li>
  <li>Here the actual problem starts its loading the same images and text.</li>
  <li>Please have a look on my bulky code and tell where I am commeting the mistake.</li>
  </ul>
  
  <p>public class Home extends Activity {
      static final String URL = "<a href="http://www.shiaislamiclibrary.com/requesthandler.ashx" rel="nofollow noreferrer">http://www.shiaislamiclibrary.com/requesthandler.ashx</a>";</p>
  
  <pre><code>static final String KEY_ITEM = "Book"; // parent node
  static final String KEY_BOOKAUTHOR = "book_author";
  static final String KEY_BOOKRATING = "BookRating";
  static final String KEY_BOOKID = "BookID";
  static final String KEY_BOOKDESC = "BookDescription";
  static final String KEY_BOOKDATEPUBLISHED = "DatePublished";
  static final String KEY_BOOKTITLE = "BookTitle";
  static final String KEY_BOOKCODE = "BookCode";
  static final String KEY_BOOKIMAGE = "BookImage";
  static final String KEY_ITEM_BOOKs_LIMIT = "Result"; // parent node
  static final String KEY_ITEM_TOTAL_BOOKS = "TotalBooks";
  
      static ArrayList&lt;String&gt; BookTitle = null;
  static ArrayList&lt;Integer&gt; BookRating = null;
  static ArrayList&lt;String&gt; BookDescription = null;
  static ArrayList&lt;String&gt; BookCoverPhotos = null;
  static ArrayList&lt;String&gt; BookAuther = null;
  static ArrayList&lt;String&gt; BookIDs = null;
  static ArrayList&lt;String&gt; BookCode = null;
  static ArrayList&lt;String&gt; BookPublishDate = null;
  static ArrayList&lt;String&gt; ImageByte = null;
  static ArrayList&lt;Bitmap&gt; bitmapArray = null;
  static int initialIndex = 12;
  
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
  setContentView(R.layout.home_activity);
  gridView = (GridView) findViewById(R.id.gridview);
  gridView.setOnScrollListener(new EndlessScrollListener());
  if (BookTitle == null) {
                  BookTitle = new ArrayList&lt;String&gt;();
                  BookRating = new ArrayList&lt;Integer&gt;();
                  BookDescription = new ArrayList&lt;String&gt;();
                  BookIDs = new ArrayList&lt;String&gt;();
                  BookCode = new ArrayList&lt;String&gt;();
                  BookCoverPhotos = new ArrayList&lt;String&gt;();
                  BookAuther = new ArrayList&lt;String&gt;();
                  BookPublishDate = new ArrayList&lt;String&gt;();
                  ImageByte = new ArrayList&lt;String&gt;();
                  bitmapArray = new ArrayList&lt;Bitmap&gt;();
                  new UIThread().execute(URL, initialIndex + "");
                  // Log.i("If", BookTitle + "");
              } else {
  
                  // Log.i("else", BookTitle + "");
                  ImageAdapter adapter2 = new ImageAdapter(getBaseContext(),
                          act);
                  gridView.setAdapter(adapter2);
  
              }
  </code></pre>
  
  <p>Im using AsynkTaks to download the images from server. here is the code</p>
  
  <pre><code>private class UIThread extends AsyncTask&lt;String, Integer, String&gt; {
  
      ProgressDialog progressDialog;
      ImageAdapter adapter = new ImageAdapter(getBaseContext(), act);
  
      @Override
      protected void onPreExecute() {
          super.onPreExecute();
  
          progressDialog = ProgressDialog.show(getParent(),
                  "Acumlating Books from server...",
                  "This may Take a few seconds.\nPlease Wait...");
      }
  
      @Override
      protected String doInBackground(String... params) {
  
          String URL = params[0];
          int initialIndex = Integer.valueOf(params[1]);
          Log.i("params", params[1] + "");
  
          XMLParser parser = new XMLParser();
          String XMLString = parser.getXmlFromUrl_FeaturedBooks(URL,
                  initialIndex);
  
          Home.initialIndex = Home.initialIndex + 12;
          Log.i("Home.initialIndex", Home.initialIndex + "");
  
          Document doc = parser.getDomElement(XMLString);
          NodeList nlBooksLimit = doc
                  .getElementsByTagName(KEY_ITEM_BOOKs_LIMIT);
          Element eLimit = (Element) nlBooksLimit.item(0);
  
          String totalBooks = parser.getValue(eLimit, KEY_ITEM_TOTAL_BOOKS);
          Log.i("totalBooks", totalBooks + "");
          NodeList nl = doc.getElementsByTagName(KEY_ITEM);
  
          // looping through all item nodes &lt;item&gt;
  
          Bitmap imageNotFound = BitmapFactory.decodeResource(getResources(),
                  R.drawable.defaultcoverphoto);
  
          for (int i = 0; i &lt; nl.getLength(); i++) {
  
              Element e = (Element) nl.item(i);
  
              try {
                  BookRating.add(Integer.valueOf(parser.getValue(e,
                          KEY_BOOKRATING)));
                  // Log.i("Rating Try", BookRating.get(i) + "");
  
              } catch (Exception e2) {
                  BookRating.add(0);
                  // Log.i("Rating Catch", BookRating + "");
              }
  
              BookDescription.add(parser.getValue(e, KEY_BOOKDESC));
              BookTitle.add(parser.getValue(e, KEY_BOOKTITLE));
              BookCoverPhotos.add("http://shiaislamicbooks.com/books_Snaps/"
                      + parser.getValue(e, KEY_BOOKCODE) + "/1_thumb.jpg");
              BookAuther.add(parser.getValue(e, KEY_BOOKAUTHOR));
              BookPublishDate.add(parser.getValue(e, KEY_BOOKDATEPUBLISHED));
              BookIDs.add(parser.getValue(e, KEY_BOOKID));
              BookCode.add(parser.getValue(e, KEY_BOOKCODE));
              // Log.i("URLs &amp; Desc", BookCoverPhotos.toString());
  
              try {
                  bookImageURL = new URL(BookCoverPhotos.get(i));
              } catch (MalformedURLException e1) {
                  e1.printStackTrace();
                  // Log.i("URL", "ERROR at image position" + i + "");
              }
  
              try {
                  bitMapImage = BitmapFactory.decodeStream(bookImageURL
                          .openConnection().getInputStream());
                  bitmapArray.add(bitMapImage);
                  publishProgress(i + 1);
  
              } catch (IOException e2) {
                  e2.printStackTrace();
                  bitmapArray.add(imageNotFound);
                  // Log.i("File Not Found", bookImageURL + "");
              }
  
          }
  
          Log.i("Book Title", BookTitle + "");
  
          return null;
      }
  
      @Override
      protected void onProgressUpdate(Integer... values) {
          super.onProgressUpdate(values);
          progressDialog.setMessage(values[0]
                  + " Book(s) found \nPlease wait...");
  
      }
  
      @Override
      protected void onPostExecute(String result) {
          super.onPostExecute(result);
          progressDialog.dismiss();
          gridView.setAdapter(adapter);
      }
  }
  </code></pre>
  
  <p>and here is the EndlessScrollListner.java code. which send the request to sever again to download when the scroll reaches to the bottom of the gridView.</p>
  
  <pre><code>private class EndlessScrollListener implements OnScrollListener {
  
      private int visibleThreshold = 0;
      private int currentPage = 0;
      private int previousTotal = 0;
      private boolean loading = true;
  
      public EndlessScrollListener() {
      }
  
      public EndlessScrollListener(int visibleThreshold) {
          this.visibleThreshold = visibleThreshold;
      }
  
      @Override
      public void onScroll(AbsListView view, int firstVisibleItem,
              int visibleItemCount, int totalItemCount) {
          if (loading) {
              if (totalItemCount &gt; previousTotal) {
                  loading = false;
                  previousTotal = totalItemCount;
                  currentPage++;
              }
          }
          if (!loading
                  &amp;&amp; (totalItemCount - visibleItemCount) &lt;= (firstVisibleItem + visibleThreshold)) {
              // I load the next page of gigs using a background task,
              // but you can call any function here.
              new UIThread().execute(URL, Home.initialIndex + "");
              Log.i("Reached", "End");
              Log.i("Home.initialIndex", Home.initialIndex + "");
              loading = true;
          }
      }
  </code></pre>
  
  <p>here are 2 pictures to make you more clear my question.
  <img src="https://i.stack.imgur.com/29T29.png" alt="enter image description here">
  <img src="https://i.stack.imgur.com/4fEhl.png" alt="enter image description here"></p>