安卓ArrayAdapter和JSONArray[英] Android ArrayAdapter and JSONArray

本文是小编为大家收集整理的关于安卓ArrayAdapter和JSONArray的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我是Android开发的新手.

我纯粹喜欢使用JSON对象和数组进行我的简单应用程序,考虑与XMLS相比,考虑JSON载体的亮度.

我对ArrayAdapter挑战填充ListView.

这就是我克服的方式,需要你的建议.

Extend the Adaptor class.
然后将jsonarray传递给构造函数.

这里的构造函数调用super 虚拟 字符串阵列设置JSONARRAY的长度.
在类中存储构造函数参数以进行进一步使用.

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 
}

现在任何改进/建议/待遇?

推荐答案

我会建议您使用Google Gson而不是JSON.它是一个库,它为您提供了来自JSON-Request的创建对象,而且您不需要每次解析JSON.只需创建一个包含json请求的所有字段的对象,并与其命名,并且无论您想要什么 - 例如:

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

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

    ]
}

您的类 - json-array项

 public class MyArrayAdapterItem{
     int id;
     String title;
 }

在您的代码中的某个地方,您正在下载数据.我不知道你是怎么做的,所以我会发布我的代码:

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());

所以这就是所有的,因为现在只是把"项目"放在适配器的构造函数

中的"项目".

其他推荐答案

您可以将null传递给超级,而不是创建字符串阵列并实现getCount方法:

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

public int getCount(){
   return profiles.length();
}

其他推荐答案

创建一个textView并使用Item.getString("key")分配,并将该字符串添加到本地字符串阵列并返回该视图

本文地址:https://www.itbaoku.cn/post/102549.html

问题描述

I am new to Android Development.

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

I had challenges with ArrayAdapter to populate the ListView.

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

Extend the Adaptor class.

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;
}

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 
}

Now Any improvements/suggestions/thoughful-question??

推荐答案

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"
        }

    ]
}

Your class - item of JSON-Array

 public class MyArrayAdapterItem{
     int id;
     String title;
 }

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());

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

其他推荐答案

You can pass null to super instead of creating a string array and implement getCount method:

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

public int getCount(){
   return profiles.length();
}

其他推荐答案

create one textview and assign with item.getString("key") and add that string to the local string array and return that view