从JSON填充列表视图 [英] Populate Listview from JSON

查看:214
本文介绍了从JSON填充列表视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有谁知道任何实例动态加载的Json 我所看到的数据到一个ListView,大多数的例子只是用 某种静态数组。我需要加载说10行 JSON数据,然后在底部有一个负载更多..获得 未来10等等等等。 使用JSON的例子请....

Does anyone know of any examples to dynamically load Json data into a ListView, most examples I have seen just use a static array of some kind. I need to load say 10 rows of Json data, then at the bottom have a load more.. to get the next 10 etc etc. Examples using Json please....

推荐答案

程序员布鲁斯是正确的,没有默认的方式做到这一点。然而,有一个非常干净的和简单的方式来获得本完成。下面是我用来处理JSONArrays适配器。

Programmer Bruce is correct, there is no default way to do this. However, there is a very clean and simple way to get this accomplished. Here is the adapter I use to handle JSONArrays.

class JSONAdapter extends BaseAdapter implements ListAdapter {

    private final Activity activity;
    private final JSONArray jsonArray;
    private JSONAdapter(Activity activity, JSONArray jsonArray) {
        assert activity != null;
        assert jsonArray != null;

        this.jsonArray = jsonArray;
        this.activity = activity;
    }


    @Override public int getCount() {

        return jsonArray.length();
    }

    @Override public JSONObject getItem(int position) {

        return jsonArray.optJSONObject(position);
    }

    @Override public long getItemId(int position) {
        JSONObject jsonObject = getItem(position);

        return jsonObject.optLong("id");
    }

    @Override public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null)
            convertView = activity.getLayoutInflater().inflate(R.layout.row, null);

        JSONObject jsonObject = getItem(position);  

        return convertView;
    }
}

这篇关于从JSON填充列表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆