1

I am currently using Android's Lazylist at https://github.com/thest1/LazyList, loading images into my listview.

The problem I faced is that the images and data loaded are correct for the first two pages. Upon reaching the third page, the data and images loaded are the same as the first page yet again, and the problem continues for the subsequent pages.

Example : Page 1

enter image description here

And when it reaches page 3,

enter image description here

Look at the date, and images loaded. It's the same. The data is grabbed off a website, and the website is sending data properly when viewed on a web browser.

Any reason the data loaded is repeated?

MainActivity

public class MainActivity extends Activity {
    XMLParser parser;
    Document doc;
    String xml;
    ListView lv;
    ListViewAdapter adapter;
    ArrayList<HashMap<String, String>> menuItems;
    ProgressDialog pDialog;
    int limit = 8;
    boolean loadingMore = false;

    String URL = "http://test.com/5.php";

    // Flag for current page
    int current_page = 1;

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {

        lv = (ListView) findViewById(R.id.list);

        menuItems = new ArrayList<HashMap<String, String>>();
        parser = new XMLParser();
        new firstLoad().execute();

        // Getting adapter
        adapter = new ListViewAdapter(this, menuItems);
        lv.setAdapter(adapter);

        lv.setOnScrollListener(new OnScrollListener() {

            public void onScrollStateChanged(AbsListView view, int scrollState) {
            }

            public void onScroll(AbsListView view, int firstVisibleItem,
                    int visibleItemCount, int totalItemCount) {

                int lastInScreen = firstVisibleItem + visibleItemCount;
                if ((lastInScreen == totalItemCount) && !(loadingMore)) {
                    new loadMoreListView().execute();
                }
            }
        });

    
    /**
     * Async Task that send a request to url Gets new list view data Appends to
     * list view
     * */
    private class loadMoreListView extends AsyncTask<Void, Void, Void> {

        @Override
        protected void onPreExecute() {
            // Showing progress dialog before sending http request
            pDialog = new ProgressDialog(MainActivity.this);
            pDialog.setMessage("Please wait..");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
        }

        protected Void doInBackground(Void... unused) {
            loadingMore = true;
            // increment current page
            current_page += 1;

            // Next page request
            URL = URL + "?page=" + current_page;

            xml = parser.getXmlFromUrl(URL); // getting XML
            doc = parser.getDomElement(xml); // getting DOM element

            NodeList nl = doc.getElementsByTagName(KEY_ITEM);
            // looping through all item nodes <item>
            for (int i = 0; i < nl.getLength(); 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_NAME, parser.getValue(e, KEY_NAME));
                map.put(KEY_IMG, parser.getValue(e, KEY_IMG));
                map.put(KEY_DATE, parser.getValue(e, KEY_DATE));
                map.put(KEY_WEBSITEURL, parser.getValue(e, KEY_WEBSITEURL));
                map.put(KEY_IMAGEURL, parser.getValue(e, KEY_IMAGEURL));
                // adding HashList to ArrayList
                menuItems.add(map);
            }

            return (null);
        }

        protected void onPostExecute(Void unused) {
            // closing progress dialog
            runOnUiThread(new Runnable() {
                public void run() {
                    // add things to do here
                    pDialog.dismiss();

                    // get listview current position - used to maintain scroll
                    // position
                    int currentPosition = lv.getFirstVisiblePosition();
                    Toast.makeText(getApplicationContext(), "Current on page " + String.valueOf(current_page), Toast.LENGTH_SHORT).show();
                    // Appending new data to menuItems ArrayList
                    adapter = new ListViewAdapter(MainActivity.this, menuItems);
                    lv.setAdapter(adapter);

                    // Setting new scroll position
                    lv.setSelectionFromTop(currentPosition + 1, 0);
                    loadingMore = false;
                }
            });

        }
    }

    private class firstLoad extends AsyncTask<Void, Void, Void> {

        @Override
        protected void onPreExecute() {
            // Showing progress dialog before sending http request
            pDialog = new ProgressDialog(MainActivity.this);
            pDialog.setMessage("Loading..");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
        }

        protected Void doInBackground(Void... unused) {
            loadingMore = true;
            xml = parser.getXmlFromUrl(URL); // getting XML
            doc = parser.getDomElement(xml); // getting DOM element

            NodeList nl = doc.getElementsByTagName(KEY_ITEM);
            // looping through all item nodes <item>
            for (int i = 0; i < nl.getLength(); 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_NAME, parser.getValue(e, KEY_NAME));
                map.put(KEY_IMG, parser.getValue(e, KEY_IMG));
                map.put(KEY_DATE, parser.getValue(e, KEY_DATE));
                map.put(KEY_WEBSITEURL, parser.getValue(e, KEY_WEBSITEURL));
                map.put(KEY_IMAGEURL, parser.getValue(e, KEY_IMAGEURL));
                // adding HashList to ArrayList
                menuItems.add(map);
            }

            // Appending new data to menuItems ArrayList

            return (null);
        }

        protected void onPostExecute(Void unused) {
            // closing progress dialog

            runOnUiThread(new Runnable() {
                public void run() {
                    loadingMore = false;
                    // add things to do here
                    pDialog.dismiss();
                    adapter = new ListViewAdapter(MainActivity.this, menuItems);
                    lv.setAdapter(adapter);
                }
            });

        }
    }
}

Last question :

Is there a code whereby it saves the state of the listview, to prevent the data from being loaded all over again? When the user presses back multiple time to end this app, and when they open it again, the data is being loaded right from page 1. Any code to save the state so it opens up where the user left off previously?

Thanks!

4

0 回答 0