简体   繁体   中英

use android dynamicaly load more items to the listview need help

package fshizzle.com;

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

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.ListActivity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.SimpleAdapter.ViewBinder;
import android.widget.Toast;

public class welcome extends ListActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Intent sender=getIntent();
        String login = sender.getStringExtra("extra_login");

        setContentView(R.layout.listplaceholder);

        ArrayList<HashMap<String, Object>> mylist = new ArrayList<HashMap<String, Object>>();    

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

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

        SimpleAdapter adapter = new SimpleAdapter(this, mylist, R.layout.json2, new String[] { "img", "name", "age" }, 
                                new int[] { R.id.item_img, R.id.item_title, R.id.item_subtitle });

        adapter.setViewBinder(new MyViewBinder());
        setListAdapter(adapter);

        final ListView lv = getListView();
        lv.setTextFilterEnabled(true);  
        lv.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {              
                @SuppressWarnings("unchecked")
                HashMap<String, String> o = (HashMap<String, String>) lv.getItemAtPosition(position);                   
                Toast.makeText(welcome.this, "ID '" + o.get("id") + "' was clicked.", Toast.LENGTH_SHORT).show(); 

            }
        });
    }

    public class MyViewBinder implements ViewBinder {
        public boolean setViewValue(View view, Object data,String textRepresentation) {
            if( (view instanceof ImageView) & (data instanceof Bitmap) ) {
                ImageView iv = (ImageView) view;
                Bitmap bm = (Bitmap) data;  
                iv.setImageBitmap(bm);  
                return true;
            }

            return false;
        }
    }



}   

this parse a json file and create a listview with image web, I would like use this code but I can not http://p-xr.com/android-tutorial-dynamicaly-load-more-items-to-the-listview-never-ending-list/

someone more experienced can tell me how to paste all this ? thank you I can not to display The infos of my json files With the Code In The url

also i try this code http://benjii.me/2010/08/endless-scrolling-listview-in-android/ but i this error "The method setOnScrollListener(welcome.EndlessScrollListener) is undefined for the type SimpleAdapter" with

adapter.setOnScrollListener(new EndlessScrollListener());
setListAdapter(adapter);

code.

With this code:

this.getListView().setOnScrollListener(new EndlessScrollListener());

no error but all is display same time. ????

What the LoadGigsTask() function ? i use // LoadGigsTask is undefined in a tutorial maybe is it because of that, no ?

it's ok in code with

((AbsListView) this.view).setOnScrollListener(new EndlessScrollListener());

But in emulator crash in this line ...

Finaly i use this i search for update the listview now ...

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)){ 
                Toast.makeText(getApplicationContext(), "OK END LIST.", Toast.LENGTH_LONG).show();
                //Thread thread =  new Thread(null, loadMoreListItems);
                //thread.start();

            }
        }
    });

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM