use android dynamicaly load more items to the listview need help

后端 未结 2 1890
深忆病人
深忆病人 2021-02-06 19:33
package fshizzle.com;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;

imp         


        
相关标签:
2条回答
  • 2021-02-06 20:01

    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 !!

    0 讨论(0)
  • 2021-02-06 20:04

    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

    0 讨论(0)
提交回复
热议问题