Android的ArrayAdapter和JSONArray [英] Android ArrayAdapter and JSONArray

查看:183
本文介绍了Android的ArrayAdapter和JSONArray的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的Andr​​oid开发。

I am new to Android Development.

我纯粹是想用JSON对象和数组来为我的简单应用考虑JSON运营商的亮度相比,XMLS。

I purely like to work with JSON Objects and Arrays for my simple application considering the lightness of the JSON Carrier compared to XMLs.

我与ArrayAdapter挑战填充ListView。

I had challenges with ArrayAdapter to populate the ListView.

这是我如何克服,需要就可以了你的建议。

This is how I overcome and need your suggestions on it.

Extend the Adaptor class.

然后通过JSONArray给构造函数。
这里的构造函数调用超 的String数组设置JSONArray的长度。
存储类的构造函数参数为将来使用。

Then pass the JSONArray to the constructor.
Here the constructor calls super with dummy String array setting the length of the JSONArray.
Store the constructor arguments in class for further use.

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建立视图。

The getView() will do the job of fetching JSONObjects from JSONArray to build view.

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 
}

现在任何改进/建议/ thoughful个问题??

推荐答案

我会建议你使用谷歌GSON而不是JSON。这是一个库,为您提供了从JSON请求创建对象,而你并不需要解析JSON everymore。只需创建一个对象,它包含了所有的领域,从您的JSON请求,并命名为相同,并用它做任何你想要的 - 例如:

I would advise you to use google GSON instead JSON. It is a library that gives you a create objects from JSON-request, and you don't need to parse JSON everymore. Just create an object which contains all the fields from your JSON request and are named the same, and do with it whatever you want - for example:

Your JSON request
{
    [
        {
            "id": "2663",
            "title":"qwe"

        },
        {
            "id": "1234",
            "title":"asd"
        },
        {
            "id": "5678",
            "title":"zxc"
        }

    ]
}

您的类 - JSON阵的项目

Your class - item of JSON-Array

 public class MyArrayAdapterItem{
     int id;
     String title;
 }

Somwhere在code,你下载数据。我不知道你怎么做,所以我会后我的code,例如:

Somwhere in your code where you downloading data. I didn't know how are you doing it so i'll post my code for example:

mGparser = new JsonParser();
Gson mGson = new Gson();

Url url = "http://your_api.com"
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("Connection", "close");
conn.connect();
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));

JsonArray request = (JsonArray) mGparser.parse(in.readLine());
in.close();
ArrayList<MyArrayAdapterItem> items = mGson.fromJson(request, new TypeToken<ArrayList<MyArrayAdapterItem>>() {}.getType());

所以,这一切,现在只是把项目,而不是JSON阵列的适配器的构造

So that's all, for now just put "items" instead JSON-array in your adapter's constructor

这篇关于Android的ArrayAdapter和JSONArray的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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