滚动时在listView中添加更多json数据

时间:2013-05-23 04:15:18

标签: android json listview scrollview endlessadapter

我用图像和文字制作了一个ListView。我能够在ListView中解析 json数据。但我的要求是加载10行Json数据,然后在底部有加载更多。以获得下一个10等等。 建议我,如果有人知道使用json的解决方案,请...

  • 以下是 Adapter.java

    的代码
    public class LazyAdapter extends BaseAdapter {
    
        private Activity activity;
        private ArrayList<HashMap<String, String>> data;
        private static LayoutInflater inflater=null;
        public ImageLoader imageLoader;
    
        public LazyAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
            activity = a;
            data=d;
            inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            imageLoader=new ImageLoader(activity.getApplicationContext());
        }
    
        public int getCount() {
            return data.size();
        }
    
        public Object getItem(int position) {
            return position;
        }
    
        public long getItemId(int position) {
            return position;
        }
    
        public View getView(int position, View convertView, ViewGroup parent) {
            View vi=convertView;
            if(convertView==null)
                vi = inflater.inflate(R.layout.list_row, null);
    
            TextView title = (TextView)vi.findViewById(R.id.title); // title
            TextView artist = (TextView)vi.findViewById(R.id.artist); // artist name
            TextView duration = (TextView)vi.findViewById(R.id.duration); // duration
            ImageView thumb_image=(ImageView)vi.findViewById(R.id.list_image);
            ImageView thumb_image1=(ImageView)vi.findViewById(R.id.imageView1);// thumb image
    
            HashMap<String, String> song = new HashMap<String, String>();
            song = data.get(position);
    
            // Setting all values in listview
            title.setText(song.get(CustomizedListView.USER_NAME));
            artist.setText(song.get(CustomizedListView.KEY_ARTIST));
            duration.setText(song.get(CustomizedListView.KEY_DURATION));
            imageLoader.DisplayImage(song.get(CustomizedListView.KEY_THUMB_URL), thumb_image);
    
            imageLoader.DisplayImage(song.get(CustomizedListView.KEY_THUMB_URL1), thumb_image1);
            return vi;
        }
    }
    
  • 以下是 MainActivity.java

    的代码
    public class CustomizedListView extends Activity {
        // All static variables
        static final String URL = XXXX.json;
        //private static String URL;
        // XML node keys
        static final String KEY_SONG = "review_full"; // parent node
        static final String KEY_ID = "activity_id";
        static final String USER_NAME = "user_name";
        static final String KEY_ARTIST = "review_small";
        static final String KEY_DURATION = "time";
        static final String KEY_THUMB_URL = "thumb";
        static final String KEY_THUMB_URL1 = "photo";
        String type;
        String user_name1;
        String thumbnail;
        String subscriber_name;
        String review_full;
        String status;
        String photo;
        ListView list;
        String review_small;
        LazyAdapter adapter;
    
    
        String[] image;
        ArrayList<HashMap<String, String>> songsList;
        public  String image1=null;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
    
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            //Intent intent1 = getIntent();
            //String easyPuzzle = intent1.getExtras().getString("Id");  
            //URL=URL0+easyPuzzle;
            songsList = new ArrayList<HashMap<String, String>>();
            JSONParser jParser = new JSONParser();
            JSONObject json = jParser.getJSONFromUrl(URL);
    
    
            try{
                Log.d("Parsing JSON Data", "Before json1 try2");
                JSONArray activities = json.getJSONArray("activities");
                Log.d("Parsing JSON Data", "after activiti");
    
                for (int i = 0; i < activities.length(); i++) {
                    JSONObject c = activities.getJSONObject(i);
                    image=new String[2];
                    Log.d("Parsing JSON Data", "inside for loop");
                    HashMap<String, String> map = new HashMap<String, String>();
    
                    //checking type content
                    type=c.getString("type");
    
                    if(type.contains("user")){
                        user_name1=c.getString( USER_NAME);
                        thumbnail=c.getString(KEY_THUMB_URL);
                        map.put(KEY_THUMB_URL,  thumbnail);
    
                        map.put(USER_NAME, user_name1+" "+"joined etable community");
                        map.put(KEY_DURATION,  c.getString( KEY_DURATION));
                        //  map.put(KEY_ARTIST,null);
    
                    }else if(type.contains("checkin")){
                        user_name1=c.getString( USER_NAME);
                        subscriber_name=c.getString("subscriber_name");
                        thumbnail=c.getString(KEY_THUMB_URL);
                        photo=c.getString("photo");
    
                        if(photo.contains("False")){
                            image1="False";
                            map.put(KEY_THUMB_URL1, image1);
                        }else{
    
                            image = photo.split("image=");
                            image1=image[1];
                            map.put(KEY_THUMB_URL1, image1);
    
                        }
    
                        map.put(KEY_THUMB_URL,  thumbnail);
                        map.put(USER_NAME, user_name1+" "+"was spotted at"+" "+subscriber_name);
                        map.put(KEY_DURATION,  c.getString( KEY_DURATION));
                        //  map.put(KEY_ARTIST,null);
    
                    }else if(type.contains("favorite")){
                        subscriber_name=c.getString("subscriber_name");
                        user_name1=c.getString( USER_NAME);
                        thumbnail=c.getString(KEY_THUMB_URL);
    
                        map.put(KEY_THUMB_URL,  thumbnail);
                        map.put(USER_NAME, user_name1+" "+"favorited"+" "+subscriber_name);
                        map.put(KEY_DURATION,  c.getString( KEY_DURATION));
                        //map.put(KEY_ARTIST,null);
    
                    }else if(type.contains("review")){
                        subscriber_name=c.getString("subscriber_name");
                        user_name1=c.getString( USER_NAME);
                        review_small=c.getString("review_small");
                        review_full=c.getString("review_full");
    
                        thumbnail=c.getString(KEY_THUMB_URL);
                        map.put(KEY_THUMB_URL,  thumbnail);
                        map.put(USER_NAME, user_name1+" "+"wrote a review for"+" "+subscriber_name);
                        map.put(KEY_DURATION,  c.getString( KEY_DURATION));
                        map.put(KEY_ARTIST,review_small);
                        map.put(KEY_SONG,review_full);
    
                    }else if(type.contains("status")){
                        user_name1=c.getString( USER_NAME);
                        status=c.getString("full_status");
    
                        thumbnail=c.getString(KEY_THUMB_URL);
                        map.put(KEY_THUMB_URL,  thumbnail);
                        map.put(USER_NAME, user_name1+" "+"says\n"+" "+status);
                        map.put(KEY_DURATION,  c.getString( KEY_DURATION));
                        //map.put(KEY_ARTIST,null);
    
                    }else{
                        user_name1=c.getString( USER_NAME);
                        map.put(USER_NAME, user_name1+" "+"some problem");
                        //map.put(KEY_ARTIST,null);
                    }
    
                    // adding HashList to ArrayList
                    songsList.add(map);
    
                }}catch (JSONException e) {
                e.printStackTrace();
            }
    
            list=(ListView)findViewById(R.id.list);
    
            // Getting adapter by passing xml data ArrayList
            adapter=new LazyAdapter(this, songsList);
            list.setAdapter(adapter);
            list.invalidateViews();
    
            // Click event for single list row
            list.setOnItemClickListener(new OnItemClickListener() {
    
                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                                        int position, long id) {
                    list.invalidateViews();
                    Log.d("Parsing JSON Data", "start click");
                    HashMap<String, String> map = songsList.get(position);
    
                    Intent in = new Intent(CustomizedListView.this, LargeView.class);
                    in.putExtra("text", map.get(USER_NAME));
                    in.putExtra("image", map.get(KEY_THUMB_URL));
                    in.putExtra("review", map.get(KEY_SONG));
    
                    Log.d("Parsing JSON Data", "inside profile33");
    
                    startActivity(in);
                    Log.d("Parsing JSON Data", USER_NAME);
    
                }
            });
        }
    
    }
    

1 个答案:

答案 0 :(得分:2)

设置你的adapter.setNotifysetchanged()将在你的ListView中显示添加的项目在这里,我举一个例子,说明如何将更多的负载与ListView集成

1.example

2.Costum Load more listview