0

我需要帮助才能使我的 Gridview OnscrollListener 正常工作。我的结果没有按预期运行。它只是加载我所有的 gridview 项目,然后当我将 gridview 滚动到底部时,它会显示一个进度对话框并刷新。

我承认我不理解 OnScrollListener() 中的逻辑。我指的是这里给出的这个例子Endless Scrolling ListView in Android。希望有人可以为我编辑代码?

我想限制每个滚动 20 个图像,直到它完成从我的 xml 加载我的所有图像。我正在从 xml 解析 url,然后将其加载到我的 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();
                }
            }
4

2 回答 2

0

更改此行

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

对此

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++) {

并在onCreate()更改

new loadGridView().execute();

new loadGridView().execute(0);

或者你会在页面*限制线上得到一个空指针异常;

于 2012-09-01T22:12:02.417 回答
0

您正在发送到 loadGridView “currentPage + 20”,然后将其乘以 limit,即 20,所以这是一个巨大的数字。你这么说

for (int i = istart; (i < nl.getLength()) && (i < iend); i++)
// for (int i = hugeNumber; (i<probablyLessNumber) && (i < iend); i++)

所以它甚至可能不会进入这个语句......我假设你应该这样:

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 + 1);
            loading = true;
        }
于 2012-09-07T08:58:40.840 回答