Android ArrayAdapter和JSONArray

 干杯彩妮子 发布于 2023-02-12 09:03

我是Android开发的新手.

考虑到JSON Carrier与XML相比的轻巧性,我纯粹喜欢使用JSON Objects和Arrays作为我的简单应用程序.

我对ArrayAdapter提出了挑战,要求填充ListView.

这就是我如何克服并需要你的建议.

Extend the Adaptor class.

然后将JSONArray传递给构造函数.
这里构造函数使用设置JSONArray长度的虚拟 String数组调用super .
将构造函数参数存储在类中以供进一步使用.

public myAdaptor(Context context, int resource, JSONArray array)
{
    super(context, resource, new String[array.length()]);
    // Store in the local varialbles to the adapter class.
    this.context = context;
    this.resource = resource;
    this.profiles = objects;
}

getView()会做的工作获取从JSONArray一个JSONObjects建立视图.

public View getView(int position, View convertView, ViewGroup parent)
{
    View view;
    if (convertView == null)
    {
        LayoutInflater inflater = (LayoutInflater) 
            context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(resource, parent, false);
    }
    else
    {
        view = convertView;
    }

    // Here 
    JSONObject item = (JSONObject) profiles.getJSONObject(position);

    // READY WITH JSONObject from the JSONArray
    // YOUR CODE TO BUILD VIEW OR ACCESS THE 
}

现在任何改进/建议/尽管问题?

撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有