从JSON填充列表视图视图、列表、JSON

2023-09-11 12:38:36 作者:孤陋寡闻

有谁知道任何实例动态加载的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;
    }
}
 
精彩推荐
图片推荐