Lazylist加载相同的数据/图像

时间:2012-09-28 16:30:56

标签: android android-listview

我目前正在https://github.com/thest1/LazyList使用Android的Lazylist,将图片加载到我的列表视图中。

我遇到的问题是前两页加载的图像和数据是正确的。到达第三页后,加载的数据和图像再次与第一页相同,后续页面的问题仍然存在。

示例: 第1页

enter image description here

当它到达第3页时,

enter image description here

查看日期和加载的图片。一样的。 数据从网站上删除,网站在网络浏览器上查看时正确发送数据。

重复加载数据的原因是什么?

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

        }
    }
}

上一个问题:

是否有代码保存列表视图的状态,以防止数据再次被加载?当用户按下多次以结束此应用程序时,当他们再次打开它时,数据正在从第1页开始加载。任何用于保存状态的代码,以便在用户之前停止的位置打开它?

谢谢!

0 个答案:

没有答案