Welcome to XDA

Search to go directly to your device's forum

Register an account

Unlock full posting privileges

Ask a question

No registration required
Post Reply

Unhappy How do I implement a onscroll Listener to my listview?

OP xemkruz2012

24th August 2014, 11:02 PM   |  #1  
OP Junior Member
Thanks Meter: 0
 
3 posts
Join Date:Joined: Mar 2014
I have a large data to load from JSON.

I have implemented a custom list view by following a tutorial, now since the data is huge I want it load as the user scrolls.

This is my LoadRestaurant class code which is inside the main activity.

Code:
class LoadRestaurants extends AsyncTask<String, String, String> {

        //Show Progress Dialog
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(SearchAll.this);
            pDialog.setMessage("Loading All Restaurants...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
        }

        protected String doInBackground(String... arg) {
            //building parameters
            List<NameValuePair> params = new ArrayList<NameValuePair>();

            //Getting JSON from URL
            String json = jsonParser.makeHttpRequest(URL_RESTAURANT_LIST, "GET", params);

            //Log Cat Response Check
            Log.d("Areas JSON: ", "> " + json);

            try {
                restaurants = new JSONArray(json);

                if (restaurants != null) {
                    //loop through all restaurants
                    for (int i = 0; i < restaurants.length(); i++) {
                        JSONObject c = restaurants.getJSONObject(i);

                        //Storing each json  object in the variable.
                        String id = c.getString(TAG_ID);
                        String name = c.getString(TAG_NAME);
                        String location = c.getString(TAG_LOCATION);
                        String rating = c.getString(TAG_RATING);

                        //Creating New Hashmap
                        HashMap<String, String>  map = new HashMap<String, String>();

                        //adding each child node to Hashmap key
                        map.put(TAG_ID, id);
                        map.put(TAG_NAME, name);
                        map.put(TAG_LOCATION, location);
                        map.put(TAG_RATING, rating);

                        //adding HashList to ArrayList
                        restaurant_list.add(map);
                    }

                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

            return null;
        }

        protected void onPostExecute(String file_url) {

            //dismiss the dialog
            pDialog.dismiss();

            //Updating UI from the Background Thread
            runOnUiThread(new Runnable() {
                @Override
                public void run() {

                    ListAdapter adapter = new SimpleAdapter(
                            SearchAll.this, restaurant_list,
                            R.layout.listview_restaurants, new String[]{
                            TAG_ID, TAG_NAME, TAG_LOCATION, TAG_RATING}, new int[]{
                            R.id.login_id, R.id.restaurant_name, R.id.address, R.id.rating});

                    setListAdapter(adapter);
                    ListView lv = getListView();


                    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                          //  Bundle bundle = new Bundle();
                             Intent intent = new Intent(SearchAll.this, RestaurantProfile.class);
                                String loginId = ((TextView) view.findViewById(R.id.login_id)).getText().toString();

                                intent.putExtra("login_id", loginId);

                            startActivity(intent);



                        }
                    });

                }
            });


        }
    }
}
I want to load around 20 restaurants and then it auto loads another 20 as soon as user reaches the end of first 20.

There are lots of tutorials online but its confusing to implement.

Please help me out!
25th August 2014, 10:58 AM   |  #2  
Junior Member
Thanks Meter: 6
 
12 posts
Join Date:Joined: Aug 2014
More
The custom ListView, support for automatic loading you can try https://github.com/chrisbanes/Android-PullToRefresh
Post Reply Subscribe to Thread
Previous Thread Next Thread
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes