use android dynamicaly load more items to the listview need help

安稳与你 提交于 2019-12-03 05:11:18

For more infos here my code use for dynamicaly load listview ;)

this.getListView().setOnScrollListener(new OnScrollListener(){

        //useless here, skip!
        public void onScrollStateChanged(AbsListView view, int scrollState) {}

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


            //what is the bottom iten that is visible
            int lastInScreen = firstVisibleItem + visibleItemCount;             

            //is the bottom item visible & not loading more already ? Load more !
            if((lastInScreen == totalItemCount) && (loadingMore)){
            Adapter.notifyDataSetChanged(); 
            }
            if((lastInScreen == totalItemCount) && !(loadingMore)){
                i++;
                //on envoie la page a aller chercher
                String page3 = getPage(var_global.URL+"listview.php?login="+login+"&page="+i);
                Log.i(LOG_TAG, page3);

                JSONObject json = JSONfunctions.getJSONfromURL(var_global.URL+"json/listview/"+login+".json");

                try{

                    JSONArray  earthquakes = json.getJSONArray("infos");

                    for(int i=0;i<earthquakes.length();i++){

                        HashMap<String, Object> map = new HashMap<String, Object>();    
                        JSONObject e = earthquakes.getJSONObject(i);

                        //si user == a endlist
                        if (e.getString("user").equals("endlistfshizzle")) {
                            loadingMore = true;
                        }
                        map.put("id",  String.valueOf(i));
                        map.put("name", "infos name:" + e.getString("user"));
                        map.put("age", "age: " +  e.getString("age"));

                        URL pictureURL = new URL(var_global.URL+"upload/thumbs/"+e.getString("image"));
                        Bitmap bitmap = BitmapFactory.decodeStream(pictureURL.openStream());
                        map.put("img",bitmap);              
                        //http://www.imagemagick.org/Usage/thumbnails/                  
                        mylist.add(map);            
                    }       
                }catch(JSONException e)        {
                     Log.e("log_tag", "Error parsing data "+e.toString());
                } catch (MalformedURLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }                           

                Adapter.notifyDataSetChanged();
            }
        }
    });                

Good programming at all !!

A list view can dynamically load items using an Adapter. The development documents have a great tutorial on how to use a ListView and GridView. Basically you set the adapter of your ListView with a collection of objects. Then when you update the objects, you can call notifyDataSetChanged.

ArrayList<String> myitems = new ArrayList<String>();
myitems.add("Hello");
myitems.add("Line 2");
myitems.add("Another line");

ListView listView = (ListView)findViewById(R.id.mylistview);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_item, myitems));
listView.setAdapter(adapter);

myitems.add("Goodbye");
adapter.notifyDataSetChanged();

R.id.mylistview is the id I gave my ListView

R.layout.list_item is the layout for each list item. It's just a simple TextView

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!