title: achieve pagination in listview in android
body: <p>I have studied various tuts on pagination in android but so far I have not implemented the concept successfully in my project.
  I have a custom list view with baseadapter and data comes in the count of 10 at a time and I also have an edit text that helps in searching the data and loading the data in the same list after clearing the previous elements.I think the problem is in my listview scroll listener.May be I have not implemented in the right way the listview does not stop loading after the loading of 10 elements.It goes again and again until all the data is not loaded.May be due to some logic.Please Help! in finding my mistake. I have posted all the necessary code here.</p>
  
  <pre><code> public class Search extends Fragment {
  
      ListView lv_search;
      Context context;
      static int pagenum = 1;
      boolean isLoading = false;
      ListViewSearchAdapter adapter;
      EditText et_search;
      VenueModel venueModel;
      ProgressDialog pdialog;
      Location latlng;
      String search;
      private boolean isDataLoad = true;
  
      ImageView map;
  
      public Search(ScreenTwo screenTwo) {
          this.context = screenTwo;
      }
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
              Bundle savedInstanceState) {
          View view = inflater.inflate(R.layout.frag_search, container, false);
          Log.e("SearchFragment", "Enter Search Fragment");
          lv_search = (ListView) view.findViewById(R.id.lv);
          et_search = (EditText) view.findViewById(R.id.et_search);
          map = (ImageView)view.findViewById(R.id.img_map);
          lv_search.setOnScrollListener(new OnScrollListener() {
              @Override
              public void onScrollStateChanged(AbsListView view, int scrollState) {}
  
              @Override
              public void onScroll(AbsListView view, int firstVisibleItem,
                      int visibleItemCount, int totalItemCount) {
  
                  int lastInScreen = firstVisibleItem + visibleItemCount;
                  if ((totalItemCount==lastInScreen) &amp;&amp; !isLoading) {
  
                      Log.e("Venue Search", "onScroll " + "lastInScreen  "
                              + lastInScreen + "firstVisibleItem  "
                              + firstVisibleItem + "visibleItemCount "
                              + visibleItemCount);
                      if (isDataLoad == true) {
                          callVenu();
                      }else if(isDataLoad == false){
                          result();
                      }
                      else{
                          Log.e("Data Ended", "Data Ended");
                      }
  
                  }
  
              }
  
          });
  
          et_search.addTextChangedListener(new TextWatcher() {
  
              @Override
              public void onTextChanged(CharSequence s, int start, int before,
                      int count) {
                  if (s.length() == 0) {
                      pagenum = 1;
                      isDataLoad = true;
                      venueModel.getDetails().clear();
                      adapter.notifyDataSetChanged();
  
                  }
  
              }
  
              @Override
              public void beforeTextChanged(CharSequence s, int start, int count,
                      int after) {
                  // TODO Auto-generated method stub
  
              }
  
              @Override
              public void afterTextChanged(Editable s) {
                  // TODO Auto-generated method stub
  
              }
          });
  
          et_search.setOnEditorActionListener(new OnEditorActionListener() {
  
              @Override
              public boolean onEditorAction(TextView v, int actionId,
                      KeyEvent event) {
                  if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                      Toast.makeText(getActivity(),
                              et_search.getText().toString(), Toast.LENGTH_SHORT)
                              .show();
  
                      search = et_search.getText().toString();
                      InputMethodManager inputManager = (InputMethodManager) getActivity()
                              .getSystemService(Context.INPUT_METHOD_SERVICE);
                      inputManager.hideSoftInputFromWindow(
                              et_search.getWindowToken(),
                              InputMethodManager.HIDE_NOT_ALWAYS);
                      if (!v.getText().toString().equals("")) {
                          pagenum = 1;
                          isDataLoad = false;
                          venueModel.getDetails().clear();
                          adapter.notifyDataSetChanged();
  
                      }
  
                  }
  
                  return false;
              }
          });
  
          return view;
  
      }
  
      private void callVenu() {
          LocationManager lm = (LocationManager) getActivity()
                  .getApplicationContext().getSystemService(
                          Context.LOCATION_SERVICE);
          latlng = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
          AsyncHttpClient client = new AsyncHttpClient();
          RequestParams params = new RequestParams();
  
          params.put("lat", latlng.getLatitude());
          params.put("lng", latlng.getLongitude());
          params.put(
                  "user_id",
                  getActivity().getApplicationContext()
                          .getSharedPreferences("pref", Context.MODE_PRIVATE)
                          .getString("user_id", ""));
          params.put("pagenum", pagenum);
          client.post("http://projectsonseoxperts.com.au/checked/api/venue",
                  params, new AsyncHttpResponseHandler() {
                      @Override
                      public void onStart() {
                          // TODO Auto-generated method stub
                          super.onStart();
  
                          pdialog = new ProgressDialog(getActivity());
                          pdialog.show();
                      }
  
                      @Override
                      public void onCancel() {
                          // TODO Auto-generated method stub
                          super.onCancel();
                          Log.e("cANCEL pAGINATION", "cANCEL pAGINATION");
                          pdialog.dismiss();
                      }
  
                      @Override
                      public void onFinish() {
                          // TODO Auto-generated method stub
                          super.onFinish();
                          pdialog.dismiss();
                      }
  
                      @Override
                      public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
  
                          Gson gson = new Gson();
                          VenueModel venueModelLocal = gson.fromJson(new String(
                                  arg2), VenueModel.class);
                          if (venueModelLocal != null
                                  &amp;&amp; venueModelLocal.getDetails().size() == 0) {
                              isLoading = true;
                              return;
                          }
  
                          if (venueModelLocal.getStatus() == true) {
                              isDataLoad = true;
                              String abc = new String(arg2);
                              Log.e("venueModel venueModel venueModel ", abc);
                              if (venueModelLocal.getKey().equals(100)) {
  
                                  isLoading = false;
                                  if (adapter == null) {
                                      venueModel = venueModelLocal;
                                      adapter = new ListViewSearchAdapter(
                                              getActivity(), venueModel);
                                      lv_search.setAdapter(adapter);
                                  } else {
  
                                      venueModel.getDetails().addAll(
                                              venueModelLocal.getDetails());
                                      adapter.notifyDataSetChanged();
                                  }
  
  
                              }
                          }
                          pagenum++;
                          Log.e("PAGENUM", pagenum + "");
                      }
  
                      @Override
                      public void onFailure(int arg0, Header[] arg1, byte[] arg2,
                              Throwable arg3) {
  
                          Log.e("onfailureLOOPJ", new String(arg2));
                          isLoading = false;
  
                      }
  
                  });
  
      }
  
      private void result() {
  
          AsyncHttpClient client = new AsyncHttpClient();
          RequestParams params = new RequestParams();
          params.put("search", search);
          params.put("lat", latlng.getLatitude());
          params.put("lng", latlng.getLongitude());
          params.put("pagenum", pagenum);
          client.post(
                  "http://projectsonseoxperts.com.au/checked/api/search-venue",
                  params, new AsyncHttpResponseHandler() {
  
                      @Override
                      public void onStart() {
                          // TODO Auto-generated method stub
                          super.onStart();
                          pdialog = new ProgressDialog(getActivity());
                          pdialog.show();
  
                      }
  
                      @Override
                      public void onCancel() {
                          // TODO Auto-generated method stub
                          super.onCancel();
                          Log.e("cANCEL pAGINATION", "cANCEL pAGINATION");
  
                          pdialog.dismiss();
                      }
  
                      @Override
                      public void onFinish() {
                          // TODO Auto-generated method stub
                          super.onFinish();
                          pdialog.dismiss();
                      }
  
                      @Override
                      public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
                          String responseString = new String(arg2);
                          VenueModel modellocal = new VenueModel();
                          Gson gson = new Gson();
                          modellocal = gson.fromJson(responseString,
                                  VenueModel.class);
  
                          if (modellocal != null
                                  &amp;&amp; modellocal.getDetails().size() == 0) {
  
                              isLoading = true;
                              return;
                          }
  
                          if (modellocal.getStatus() == true) {
                              isDataLoad = false;
                              String abc = new String(arg2);
                              Log.e("venueModel venueModel venueModel ", abc);
                              if (modellocal.getKey().equals(100)) {
                                  isLoading = false;
                                  if (adapter == null) {
                                      venueModel = modellocal;
                                      adapter = new ListViewSearchAdapter(
                                              getActivity(), venueModel);
                                      lv_search.setAdapter(adapter);
                                  } else {
  
                                      venueModel.getDetails().addAll(
                                              modellocal.getDetails());
                                      adapter.notifyDataSetChanged();
                                  }
                                  // lv_search.invalidateViews();
  
  
                              }
                          }
                          pagenum++;
                          Log.e("SEARCHPAGE", pagenum + "");
                      }
  
                      @Override
                      public void onFailure(int arg0, Header[] arg1, byte[] arg2,
                              Throwable arg3) {
  
                          isLoading = false;
                          isDataLoad = false;
                      }
                  });
      }
  
  
  
  }
  </code></pre>