Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8477733
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T18:32:44+00:00 2026-06-10T18:32:44+00:00

I need help to make my Gridview OnscrollListener to work properly. My outcome is

  • 0

I need help to make my Gridview OnscrollListener to work properly. My outcome is not functioning as intended. It just loads all my gridview items, then when I scroll the gridview to the bottom it shows a progress dialog and refreshes.

I do admit that I do not understand the logic in OnScrollListener(). I’m referring to this example given here Endless Scrolling ListView in Android . Hopefully someone could edit the codes for me?

I would like to limit 20 images per scroll until it finishes to load all my images from my xml. I’m parsing urls from xml, then it loads into my Gridview.

GridViewActivity.class

public class GridViewActivity extends Activity {
        static final String URL = "http://api.androidhive.info/music/music.xml";
        private ProgressDialog pDialog;
        ArrayList<HashMap<String, String>> songsList;
        static final String KEY_SONG = "song"; 
        static final String KEY_ID = "id";
        static final String KEY_CAT_ARTIST = "artistcat";
        static final String KEY_THUMB_URL = "thumb_url";
        static final String KEY_BIG_URL = "big_url";
        static final String KEY_CAT_URL = "cat_url";
        GridView grid;
        GridViewActivityLazyAdapter adapter;
        String cat_url;
        String artist_url;
        private int visibleThreshold = 5;
        private int currentPage = 0;
        private int previousTotal = 0;
        private boolean loading = true;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.gridview_main);

            new loadGridView().execute();

            grid = (GridView) findViewById(R.id.grid_view);

        grid.setOnScrollListener(new OnScrollListener() {

        @Override
        public void onScroll(AbsListView view, int firstVisibleItem,
                int visibleItemCount, int totalItemCount) {
            // TODO Auto-generated method stub
            if (loading) {
                if (totalItemCount > previousTotal) {
                    loading = false;
                    previousTotal = totalItemCount;
                    currentPage++;
                }
            }
            if (!loading && (totalItemCount - visibleItemCount) <= (firstVisibleItem + visibleThreshold)) {
                // I load the next page of gigs using a background task,
                // but you can call any function here.
                new loadGridView().execute(currentPage + 20);
                loading = true;
            }
        }

        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {
            // TODO Auto-generated method stub
            }
        });

    }

        public class loadGridView extends AsyncTask<Integer, String, String> {

            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                pDialog = new ProgressDialog(GridViewActivity.this);
                pDialog.setTitle("Connect to Server");
                pDialog.setIndeterminate(false);
                pDialog.setCancelable(false);
                pDialog.show();
            }
            @Override
            protected String doInBackground(Integer... args) {
                // updating UI from Background Thread
                        Intent in = getIntent();
                        songsList = new ArrayList<HashMap<String, String>>();
                        cat_url = in.getStringExtra(KEY_CAT_URL);
                        artist_url = in.getStringExtra(KEY_CAT_ARTIST);


                        XMLParser parser = new XMLParser();
                        String xml = parser.getXmlFromUrl(URL); // getting XML from URL
                        Document doc = parser.getDomElement(xml); // getting DOM element

                        NodeList nl = doc.getElementsByTagName(KEY_SONG);
                        // looping through all song nodes <song>
                        int page = args[0];
                int limit = 20; // items per page
                int istart = page * limit; // what item to start on (onCreate() calls it with 0 and 0 * 20 = 0)
                int iend = istart + limit; // how far to go
                for (int i = istart; (i < nl.getLength()) && (i < iend); i++) {
                            // creating new HashMap
                            HashMap<String, String> map = new HashMap<String, String>();
                            Element e = (Element) nl.item(i);
                            // adding each child node to HashMap key => value
                            map.put(KEY_ID, parser.getValue(e, KEY_ID));
                            map.put(KEY_THUMB_URL, parser.getValue(e, KEY_THUMB_URL));
                            map.put(KEY_BIG_URL, parser.getValue(e, KEY_BIG_URL));
                            // adding HashList to ArrayList
                            songsList.add(map);

                        }
                        return null;
                    }   

            @Override
            protected void onPostExecute(String args) {
                adapter=new GridViewActivityLazyAdapter(GridViewActivity.this, songsList);   
                grid.setAdapter(adapter);
                pDialog.dismiss();
                }
            }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T18:32:45+00:00Added an answer on June 10, 2026 at 6:32 pm

    Change this line

    for (int i = 0; i < nl.getLength(); i++) {
    

    to this

    int page = args[0];
    int limit = 20; // items per page
    int istart = page * limit; // what item to start on (onCreate() calls it with 0 and 0 * 20 = 0)
    int iend = istart + limit; // how far to go
    for (int i = istart; (i < nl.getLength()) && (i < iend); i++) {
    

    and in onCreate() change

    new loadGridView().execute();
    

    to

    new loadGridView().execute(0);
    

    or you will get a null pointer exception on the page * limit line;

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to make an overlay Just like this Attached Pic... Need help to
i need help on how to make fixed textview size in table layout. say
I need help on figuring how to make a link for my Product that
This is beyond my level and I need some help. In the htaccess make
I am trying to make SELECT statement for following situation and need help to
I need help to make fileupload.cs reusable for uploading gallery images in content/gallery folder
First of all I am in DESPERATE need of help here PLEASE I will
I need some help with make a datatable from sql. I'm a Newbie. I
i try to build a menu and i need some help to make it
I need help, I need to make a menu that only shows when you

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.