0

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.

 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) && !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
                                && 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
                                && 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;
                    }
                });
    }



}
TUSHAR
  • 327
  • 1
  • 10
  • possible duplicate of [Implement Endless scroll on ListView](http://stackoverflow.com/questions/12583419/implement-endless-scroll-on-listview) – Karn Shah Apr 14 '15 at 06:32
  • probably at that place = > venueModel.getDetails().addAll( venueModelLocal.getDetails()); adapter.notifyDataSetChanged(); You should set your new list in adapterobject – Piotr Golinski Apr 14 '15 at 06:34

2 Answers2

0

You should use endless scroll listener. This link help you for how to use endless scroll listener. https://github.com/codepath/android_guides/wiki/Endless-Scrolling-with-AdapterViews

first page id [1,2,3,4,5]

after you send id 5 to server

second page id[6,7,8,9,10]

msevgi
  • 4,828
  • 2
  • 24
  • 30
  • Yes I have used that concept but I have a problem in handling the webservices in it as I have used two.....one for venue items and other for search items... – TUSHAR Apr 14 '15 at 07:39
  • my Listview continuously loading the data in the start..it sholud load only 10 items per page in the start..How can I achieve it. – TUSHAR Apr 14 '15 at 07:51
  • you must send received last item id to server. And server return, from your last id to your page size. After, you must addAll data your list and notify adapter. – msevgi Apr 14 '15 at 08:37
-2

Please consider using CommonWare's excellent https://github.com/commonsguy/cwac-endless.

TMS
  • 1,201
  • 1
  • 13
  • 20